matplotlib
๊ฐ์
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๋ ์๋์ ๊ฐ์ด ํฌ๊ฒ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฆ, 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