matplotlib

Published by onesixx on

https://matplotlib.org/

๊ฐœ์š”

https://matplotlib.org/stable/users/explain/quick_start.html

Figure : ์•ก์ž, ํ‹€

Axes : ์ปจ๋ฒ„์Šค, ๊ทธ๋ ค์งˆ ๊ณต๊ฐ„

  • title
  • Axis : ์ถ•
  • x label
import matplotlib.pyplot as plt
fig       = plt.figure()       # axes์—†์ด ๋นˆ figure

fig, ax   = plt.subplots()     # single axes์˜ figure    1 
fig, axes = plt.subplots(2,3)  # 2*3 gird axes์˜ figure  2

๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• – stateless API ์‚ฌ์šฉ

์ถœ์ฒ˜ :matplotlib ์™„๋ฒฝ ์ •๋ฆฌ

๊ธฐ์กด์˜ย pltย ๊ธฐ๋ฐ˜ ๊ทธ๋ž˜ํ”„ ์ฝ”๋“œ๋ฅผย fig์™€ย axย ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ•˜๋ผ.

https://matplotlib.org/stable/users/explain/quick_start.html

fig      = plt.figure() 
ax.plot( [1,2,3,4], [1,4,2,3])
fig, ax = plt.subplots()
ax.scatter(mtcars["wt"], mtcars["mpg"])

matplotlib๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

stateful API (state-based):

์ฆ‰, figure๋‚˜ ax๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์Œ.

x = [ 2,5,7,9]
y = [10,2,7,4]

plt.figure(figsize=(10, 5))
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.suptitle('Line Plot');

import matplotlib.pyplot as plt

(๋ฐ์ดํ„ฐ๋งŒ ๋˜์ง€๊ณ , ๋‚ด๋ถ€์ ์œผ๋กœ ์•”๋ฌต์ ์œผ๋กœ ์„ค์ •๋จ)
==> ๋น„์ถ”, TEST์šฉ, ๊ต์œก์šฉ

 stateless API (objected-based) :

๋‚ด๊ฐ€ ์ง€์ •ํ•œ figure, ๋‚ด๊ฐ€ ์ง€์ •ํ•œ ax์— ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•

x = [ 2,5,7,9]
y = [10,2,7,4]

fig, ax=plt.subplots(figsize=(10, 5))
ax.plot(x,y)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Line Plot')

fig, ax๋ฅผ object๋กœ ๋งŒ๋“ค๊ณ  ํ•ด๋‹น ๋ณ€์ˆ˜์— setํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ

ํ˜„์žฌ์˜ fig, ax๋งŒ ์‚ฌ์šฉํ•˜๊ณ , api๋ฅผ ํ†ตํ•ด ์„ค์ •
stateless API (= object oriented) fig,ax

Matplotlib์˜ component ๊ฐ๊ฐ์„ Object๋กœ ๋ฐ›์•„, ํ•จ์ˆ˜์‹คํ–‰/ ์†์„ฑ์„ค์ • get/set ํ•˜๋Š” ๋ฐฉ์‹
=> figure, ax(es)๋ฅผ ์šฐ์„  ์ƒ์„ฑํ•˜๊ณ , ํ•˜๋‚˜์”ฉ ๋”ํ•ด๊ฐ€๋Š” ๋ฐฉ์‹

* figure ์™€ ax ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•

fig = plt.figure() # ax ์—†๋Š” ๋นˆ figure ์ƒ์„ฑ (ํ›„์— ax๋ฅผ ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•จ)

fig, ax = plt.subplots(figsize=(15,10)) # ํ•˜๋‚˜์˜ ax ๋งŒ์„ ๊ฐ€์ง€๋Š” ํ•˜๋‚˜์˜ figure ์ƒ์„ฑ 

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10,10)) # 4๊ฐœ(2*2)์ด ax๋“ค์„ ๊ฐ€์ง€๋Š” ํ•˜๋‚˜์˜ figure ์ƒ์„ฑ
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# import seaborn as sns
# sns.set_theme(style="whitegrid")

# Load data
x = np.linspace(-2, 2, 1000)
y1 = np.cos(40 * x)
y2 = np.exp(-x**2)

# ๋ฐฉ๋ฒ•1: figure, ax ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ๋งŒ ๊ฐ€์ง€๋Š” ๊ทธ๋ž˜ํ”„  (ax์˜ ์ƒ‰๊น” ์ง€์ • ๋ชปํ•จ)
fig, ax = plt.subplots(figsize=(8, 2.5), facecolor="skyblue")

# # ๋ฐฉ๋ฒ•2: ๋นˆ figure๋ฅผ ์ƒ์„ฑํ•˜๊ณ  => ๊ทธ ์•ˆ์— ax๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ
# fig = plt.figure(figsize=(14, 10), facecolor="red") 
# ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], facecolor="blue")  # l b w h์ถ”๊ฐ€

# ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ, ๊ณ„์† addํ•˜๋ฉฐ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Œ
ax.plot(x, y1*y2) # ax ์— plotting
ax.plot(x, y2, 'go--') # ๋งˆ์ปค ์„ค์ • : color: green  b r k y , marker: circle o s ^ * + , linestyle: dashed line - -. :
ax.plot(x, -y2, 'g')
ax.set_xlabel("x") # x์ถ• ์ด๋ฆ„ ์„ค์ •
ax.set_ylabel("y") # y์ถ• ์ด๋ฆ„ ์„ค์ •
fig

# ax.plot(x, y1*y2) # ax ์— plotting
# fig
# ax.plot(x, y2, 'go--')
# fig
# ax.plot(x, -y2, 'g')
# fig
# ax.set_xlabel("x") # x์ถ• ์ด๋ฆ„ ์„ค์ •
# ax.set_ylabel("y") # y์ถ• ์ด๋ฆ„ ์„ค์ •
# fig
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# import seaborn as sns
# sns.set_theme(style="whitegrid")

# Load data
x1 = np.random.randn(100)     # 100
x2 = np.random.randn(100)     # 100
r = np.linspace(0, 5, 100)    # 100
a = 4 * np.pi * r ** 2        # 100  area  
v = (4 * np.pi / 3) * r ** 3  # 100  volume
x = np.linspace(0, 50, 500)   # 500
y = np.sin(x) * np.exp(-x/10) # 500

# ์—ฌ๋Ÿฌ๊ฐœ์˜ ax๋ฅผ ๊ฐ€์ง€๋Š” ํ•˜๋‚˜์˜ figure ์ƒ์„ฑ
fig, axes = plt.subplots(2, 2, figsize=(10, 10)) # 4๊ฐœ์˜ ax๋ฅผ ๊ฐ€์ง„ ํ•˜๋‚˜์˜ figure ์ƒ์„ฑ

# 0,0 ax์— ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ
axes[0, 0].plot(r, a, lw=2, color="blue")       # lw: line width 
axes[0, 0].set_title("Draw twinx", fontsize=16) # ax ์ค‘์—์„œ 0ํ–‰ 0์—ด์˜ ์ œ๋ชฉ ์„ค์ •
axes[0, 0].set_xlabel("radius [m]", fontsize=16)
axes[0, 0].set_ylabel(r"surface area ($m^2$)", fontsize=16, color="blue") # ์ˆ˜ํ•™์‹์„ ๋‚˜ํƒ€๋‚ผ๋Œ€ $ ์‚ฌ์ด์— ๋„ฃ์Œ
for label in axes[0, 0].get_yticklabels(): # y์ถ• tick ์ƒ‰๊น” ์ง€์ •
    label.set_color("blue")
    
ax2 = axes[0, 0].twinx() # ์ด์ค‘์ถ• ๊ทธ๋ฆฌ๊ธฐ
ax2.plot(r, v, lw=2, color="red")
ax2.set_ylabel(r"volume ($m^3$)", fontsize=16, color="red") 
for label in ax2.get_yticklabels():
    label.set_color("red")

# 0,1 ax์— ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ
axes[0, 1].plot(x, np.sin(x) * np.exp(-x/10), lw=2) 
axes[0, 1].set_xlabel("x",    labelpad=5, fontsize=18, fontname='serif', color="blue") # label ์œ„์น˜, ์ƒ‰๊น”, ๊ธ€๊ผด ์กฐ์ ˆ
axes[0, 1].set_ylabel("f(x)", labelpad=1, fontsize=18, fontname='serif', color="blue")
axes[0, 1].set_title("axis label", loc='left', fontsize=16, fontname='serif', color="blue") # ์ œ๋ชฉ ์™ผ์ชฝ ์ •๋ ฌ
axes[1, 0].set_title("axis('equal')") 

# 1,0 ax์— ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ
axes[1, 0].scatter(x1, -x1 + x2) 
axes[1, 0].axis('equal') # x,y์ถ•์˜ ๋ฒ”์œ„๋ฅผ ๋™์ผํ•˜๊ฒŒ ํ•˜๊ธฐ
# 1,1 ax์— ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ

# subplot ๊ฐ„ ๊ฐ„๊ฒฉ ์กฐ์ ˆ

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.html

github.com/Apress/numerical-python-second-ed

Categories: visualization

onesixx

Blog Owner

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x