[๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ&์๊ฐํ 5] ๋ฐ์ดํฐ ์๊ฐํ (Matplotlib) - ๋ผ์ธ, ๋ฐ, ํ์คํ ๊ทธ๋จ, ํ์ด
# ๋ฐ์ดํฐ ๊ทธ๋ํ์ ์ข ๋ฅ
< 1. Line Plot >
์ฐ์ํ ๋ฐ์ดํฐ
๋ฐ์ดํฐ์ ๋ณํ ๋ฐ ์ถ์ด๋ฅผ ์๊ฐํ
# ์ ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ
๋งทํ๋กฏ๋ฆฝ์ plot() ํจ์๋ ์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆด ์ ์๋ค
์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์์๋ x์ถ์ ๊ฐ, ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์์๋ y์ถ์ ํด๋นํ๋ ๊ฐ์ ์ ๋ฌํ๋ค
# ๋งทํ๋กฏ๋ฆฝ์ผ๋ก ๊ทธ๋ํ ๊ทธ๋ ค๋ณด๊ธฐ (1)
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [2,4,6,8,10]
plt.plot(x,y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Example')
# ๋งทํ๋กฏ๋ฆฝ์ผ๋ก ๊ทธ๋ํ ๊ทธ๋ ค๋ณด๊ธฐ (2)
๋ฐ์ดํฐํ๋ ์์์ ๊ทธ๋ฆฌ๊ธฐ
import pandas as pd
df = pd.DataFrame({
'A' : [1,2,3,4,5],
'B' : [5,4,3,2,1]
})
df
df.plot(x='A', y='B')
plt.show()
# ๊ทธ๋ํ ์คํ์ผ ์ค์ ํ๊ธฐ
df.plot(x='A', y='B', color = 'green', linestyle = '--', marker = 'o')
plt.show()
โ๏ธlinestyle ์ข ๋ฅ
์ค์ : '-'
์ ์ : ':'
์์ : '-.'
ํ์ : '--'
# ๋ฒ๋ก ์ถ๊ฐํ๊ธฐ (1)
df.plot(x='A', y='B', color = 'red', linestyle = '--', marker = 'o', label = 'Data Series')
plt.show()
# ๋ฒ๋ก ์ถ๊ฐํ๊ธฐ (2)
ax = df.plot(x='A', y='B', color='red', linestyle='--', marker='o')
ax.legend(['Data Series'])
plt.show()
# ๋ฒ๋ก ์์
ax = df.plot(x='A', y='B', color='red', linestyle='--', marker='o')
ax.legend(['Data Series'])
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Title')
plt.show()
# ์์ ํ ์คํธ ๋ฃ๊ธฐ
text() ๋ฉ์๋ ์ด์ฉ
ax = df.plot(x='A', y='B', color='red', linestyle='--', marker='o')
ax.legend(['Data Series'])
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Title')
ax.text(4, 3, 'SomeText', fontsize=12)
plt.show()
# ๊ทธ๋ํ ํฌ๊ธฐ ๋ฐ๊พธ๊ธฐ : figsize ๋งค๊ฐ๋ณ์
plt.figure(figsize = (8,6))
x = [1,2,3,4,5]
y = [2,4,6,8,10]
plt.plot(x,y)
plt.show()
# ๊ด๋ จ ์์
fig, ax = plt.subplots(figsize = (18,6))
ax = df.plot(x='A', y='B', color='red', linestyle='--', marker='o', ax = ax)
ax.legend(['Data Series'])
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Title')
ax.text(4, 3, 'SomeText', fontsize=12)
plt.show()
# ์์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
import seaborn as sns
data = sns.load_dataset('flights')
# ๋ฐ์ดํฐ year๋ณ passengers ์์ ํฉ๊ณ๋ฅผ ๊ทธ๋ฃน๋ณ๋ก ์ถ๋ ฅํ๊ธฐ
data_grouped = data[['year', 'passengers']].groupby('year').sum().reset_index()
data_grouped
# ํด๋น ๋ด์ฉ์ ์ ๊ทธ๋ํ๋ก ๊ทธ๋ ค๋ณด๊ธฐ
plt.plot(data_grouped['year'], data_grouped['passengers'])
plt.xlabel('year')
plt.ylabel('passengers')
plt.show()
< 2. Bar Plot >
๋ฒ์ฃผํ ๋ฐ์ดํฐ
์นดํ ๊ณ ๋ฆฌ ๋ณ ๊ฐ์ ํฌ๊ธฐ๋ฅผ ์๊ฐ์ ์ผ๋ก ๋น๊ต
# ๋ง๋ ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ
๋งทํ๋กฏ๋ฆฝ์ bar() ํจ์๋ ๋ง๋ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆด ์ ์๋ค
์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์์๋ x์ถ์ ๊ฐ, ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์์๋ y์ถ์ ํด๋นํ๋ ๊ฐ์ ์ ๋ฌํ๋ค
# ์ํ ๋ฐ์ดํฐ
df = pd.DataFrame({
'๋์' : ['์์ธ', '๋ถ์ฐ', '๋๊ตฌ', '์ธ์ฒ'],
'์ธ๊ตฌ' : [990, 250, 250, 290]
})
df
# bar()๋ฅผ ์ด์ฉํ ๋ง๋๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ
plt.bar(df['๋์'], df['์ธ๊ตฌ'])
plt.xlabel('๋์')
plt.ylabel('์ธ๊ตฌ')
plt.title('๋์๋ณ ์ธ๊ตฌ ์')
< 3. Histogram >
์ฐ์ํ ๋ฐ์ดํฐ
๋ฐ์ดํฐ์ ๋ถํฌ, ๋น๋, ํจํด ๋ฑ์ ์ดํด
# ํ์คํ ๊ทธ๋จ ์์
import numpy as np
data = np.random.randn(1000)
plt.hist(data, bins=30)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()
< 4. Pie Chart >
๋ฒ์ฃผํ ๋ฐ์ดํฐ์ ๋น์จ
๋ฒ์ฃผ๋ณ ์๋์ ๋น์จ์ ๋ถ์ฑ๊ผด ๋ชจ์์ผ๋ก ์๊ฐํ
# ํ์ด์ฐจํธ ์์
sizes = [30, 20, 25, 15, 10]
labeling = ['A', 'B', 'C', 'D', 'E']
plt.pie(sizes, labels = labeling)
plt.title('Pie Chart')
plt.show()