Knowledge๐Ÿฆข/๋ฐ์ดํ„ฐ ๋ถ„์„

[๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ&์‹œ๊ฐํ™” 5] ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” (Matplotlib) - ๋ผ์ธ, ๋ฐ”, ํžˆ์Šคํ† ๊ทธ๋žจ, ํŒŒ์ด

ํŒŒ์นดํŒŒ์˜ค 2024. 5. 12. 17:53

 

# ๋ฐ์ดํ„ฐ ๊ทธ๋ž˜ํ”„์˜ ์ข…๋ฅ˜

 

< 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()