* [์คํจ ์์ด ์์ฃผํ๋ ํ์ด์ฌ ๋ฐ์ดํฐ ๋ถ์ ์
๋ฌธ] ๊ฐ์๋ฅผ ์๋น ๋ถ๋ถ ์ฐธ๊ณ ํ์ฌ ์์ฑํ์์ต๋๋ค.
!pip install matplotlib
import matplotlib.pyplot as plt
โ
plt.plot([2, 3, 4, 5]) # 1๊ฐ ๋ฆฌ์คํธ ์ฝ์
์ y ๊ฐ์ผ๋ก ์ธ์
# ๋ฆฌ์คํธ ์ธ ํํ, Numpy array ์
๋ ฅ ๊ฐ๋ฅ
plt.show() # ๊ทธ๋ํ๋ฅผ ํ๋ฉด์ ์ถ๋ ฅ
plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) # ๊ฐ๊ฐ์ ๋ฆฌ์คํธ๋ ๊ฐ๊ฐ x, y ๊ฐ์ผ๋ก ์ธ์
plt.show()
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.xlabel('X-Label')
plt.ylabel('Y-Label')
plt.show()
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], label = 'Square')
plt.xlabel('X-Label')
plt.ylabel('Y-Label')
plt.legend()
plt.show()
plt.plot([1, 2, 3, 4], [3, 6, 9, 12])
plt.xlabel('X-Label')
plt.ylabel('Y-Label')
plt.xlim([0, 5])
plt.ylim([0, 15])
plt.show()
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.axis([0, 6, 0, 20])
plt.show()
'-' (Solid), '- -' (Dashed), ' : ' (Dotted), ' -. ' (Dash-dot)
solid, dashed, dotted, dashdot
(0, (1, 1)) [dotted], (0, (5, 5)) [dashed], (0, (3, 5, 1, 5)) [dashdotted]
# plot() ํจ์์ ํฌ๋งท ๋ฌธ์์ด ์ฌ์ฉ
plt.plot([1, 2, 3], [4, 4, 4], '-', color='C0', label='Solid')
plt.plot([1, 2, 3], [3, 3, 3], '--', color='C0', label='Dashed')
# plot() ํจ์์ linestyle ๊ฐ์ผ๋ก ์ฝ์
plt.plot([1, 2, 3], [2, 2, 2], linestyle='dotted', color='C0', label='Dotted')
plt.plot([1, 2, 3], [1, 1, 1], linestyle='dashdot', color='C0', label='Dash-dot')
plt.xlabel('X-Label')
plt.ylabel('Y-Label')
plt.axis([0.8, 3.2, 0.5, 5.0])
plt.legend(loc='upper right', ncol=4)
plt.show()
# ํํ์ ์ฌ์ฉํ์ฌ ์ ์ ์ข
๋ฅ ์ปค์คํฐ๋ง์ด์ฆ
plt.plot([1, 2, 3], [4, 4, 4], linestyle=(0, (1, 1)), color='C0', label='(0, (1, 1))')
plt.plot([1, 2, 3], [3, 3, 3], linestyle=(0, (1, 5)), color='C0', label='(0, (1, 5))')
plt.plot([1, 2, 3], [2, 2, 2], linestyle=(0, (5, 1)), color='C0', label='(0, (5, 1))')
plt.plot([1, 2, 3], [1, 1, 1], linestyle=(0, (3, 5, 1, 5)), color='C0', label='(0, (3, 5, 1, 5))')
plt.xlabel('X-Label')
plt.ylabel('Y-Label')
plt.axis([0.8, 3.2, 0.5, 5.0])
plt.legend(loc='upper right', ncol=2)
plt.show()
- 'roโ๋ ๋นจ๊ฐ์ (โredโ)์ ์ํ (โcircleโ) ๋ง์ปค๋ฅผ ์๋ฏธ
- 'k^โ๋ ๊ฒ์ ์ (โblackโ)์ ์ผ๊ฐํ (โtriangleโ) ๋ง์ปค๋ฅผ ์๋ฏธ
- 's'(square), 'D'(diamond), '$ ๋ฌธ์ $'(๋ฌธ์ ๋ง์ปค)
plt.plot([4, 5, 6], "b")
plt.plot([3, 4, 5], "ro")
plt.plot([2, 3, 4], marker="s")
plt.plot([1, 2, 3], marker="D")
plt.plot([0, 1, 2], marker='$A$') # A ๋ชจ์ ๋ง์ปค
plt.show()
plt.plot([1, 2, 3, 4], [2.0, 3.0, 5.0, 10.0], 'r')
plt.plot([1, 2, 3, 4], [2.0, 2.8, 4.3, 6.5], color = 'violet')
plt.plot([1, 2, 3, 4], [2.0, 2.5, 3.3, 4.5], color = 'dodgerblue')
plt.xlabel('X-Label')
plt.ylabel('Y-Label')
plt.show()
loc ํ๋ผ๋ฏธํฐ : {โleftโ, โcenterโ, โrightโ}
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.xlabel('X-Label')
plt.ylabel('Y-Label')
plt.title('Graph Title', loc='center', pad=20)
plt.show()
# years๋ X์ถ์ ํ์๋ ์ฐ๋, values๋ ๋ง๋ ๊ทธ๋ํ์ y ๊ฐ
# xticks(x, years) : x์ถ์ ๋๊ธ ๋ ์ด๋ธ์ '2022', '2023', '2024' ์์๋๋ก ์ค์
# color์ width๋ก ๋ง๋ ๊ทธ๋ํ ํ๋ผ๋ฏธํฐ ์ค์
x = [1, 2, 3]
years = ['2022', '2023', '2024']
values = [300, 100, 700]
plt.bar(x, values, color=['r', 'g', 'b'], width=0.4)
#plt.bar(x, values, color=['r', 'g', 'b'], width=0.8)
plt.xticks(x, years)
plt.show()
# numpy์ random ๋ชจ๋์ rand ํจ์๋ฅผ ํตํด ์ซ์ ๋๋คํ๊ฒ ์์ฑ
# color์ size๋ก ์ฐ์ ๋ ํ๋ผ๋ฏธํฐ ์ค์
import numpy as np
np.random.seed(0)
n = 50
x = np.random.rand(n)
y = np.random.rand(n)
size = (np.random.rand(n) * 20)**2
colors = np.random.rand(n)
plt.scatter(x, y, s=size, c=colors)
plt.show()
# subplot
# nrows=2, ncols=1, index=1
plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'o-')
plt.title('1st Graph')
# subplot
# nrows=2, ncols=1, index=2
plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.title('2nd Graph')
plt.tight_layout()
plt.show()
plt.subplots() ํจ์๋ ์ฌ๋ฌ ๊ฐ ๊ทธ๋ํ๋ฅผ ํ ๋ฒ์ ์๊ฐํ ๊ฐ๋ฅ
plt.subplots() ํจ์์ ๋ํดํธ ํ๋ผ๋ฏธํฐ๋ 1์ด๋ฉฐ ์ฆ plt.subplots(nrows=1, ncols=1) ์๋ฏธ
plt.subplots() ํจ์๋ figure์ axes ๊ฐ์ ๋ฐํ
- figure : ์ ์ฒด subplot ์๋ฏธ
- ์๋ธํ๋กฏ ์์ ๋ช ๊ฐ์ ๊ทธ๋ํ๊ฐ ์๋์ง ์๊ด์์ด ๊ทธ๊ฑธ ๋ด๋ ์ ์ฒด ์ฌ์ด์ฆ๋ฅผ ์๋ฏธ- axe : ์ ์ฒด ์ค ๋ฑ๋ฑ๊ฐ ์๋ฏธ
ex) ์๋ธํ๋กฏ ์์ 2๊ฐ(a1,a2)์ ๊ทธ๋ํ๊ฐ ์๋ค๋ฉด a1, a2 ๋ฅผ ์ผ์ปฌ์
.twinx() ํจ์๋ ax1๊ณผ ์ถ์ ๊ณต์ ํ๋ ์๋ก์ด Axes ๊ฐ์ฒด ์์ฑ
fig, ax1 = plt.subplots()
# -s(solid line style + square marker), alpha(ํฌ๋ช
๋)
ax1.plot(x, y1, '-s', color='green', markersize=7, linewidth=5, alpha=0.7)
# .twinx() ํจ์๋ ax1๊ณผ ์ถ์ ๊ณต์ ํ๋ ์๋ก์ด Axes ๊ฐ์ฒด ์์ฑ
ax2 = ax1.twinx()
ax2.bar(x, y2, color='deeppink', alpha=0.7, width=0.7)
#plt.twinx()
#plt.bar(x, y2, color='deeppink', alpha=0.5)
plt.show()
fig, ax = plt.subplots(nrows=2, figsize=(5, 5), constrained_layout=True)
sns.countplot(data=df_ex2, y='Sex', hue='Survived', palette='Set1', ax=ax[0])
ax[0].legend(labels=['dead', 'survivors'])
ax[0].set(xlabel='', ylabel='', title='Number of dead & survivors')
sns.barplot(data=df_ex2, x='Pclass', y='Survived', hue='Sex', palette='Set1', ax=ax[1], errorbar=None)
ax[1].legend(title='')
ax[1].set(title='Survival rate', ylabel='')