노트1/Python

[Python] matplotlib 정리

Paige09 2022. 1. 10. 17:53

 

import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.family'] = 'Malgun Gothic'

 


 

1/   내가 지정한 도화지(figure), 위치(ax)에 그림 그리기

 

 

figure > 하나의 그래프

fig, ax = plt.subplots()

fig, ax = plt.subplots(figsize=(5,3))

train['Survived'].value_counts().plot.bar(fontsize=10, color='g', rot=0)
ax.set(xticklabels=['사망','생존'])

 

 

figure > 여러개의 그래프

fig, ax = plt.subplots(n,m)

fig, ax = plt.subplots(2,2, figsize=(7,5)) #도화지
labels = ['사망','생존']

train['Survived'].value_counts().plot.bar(ax=ax[0,0], color='g', rot=0)
# ax[0,0].set(xlabel='Survived', xticklabels=labels, ylabel = 'Count')

train['Survived'].value_counts().plot.pie(ax=ax[1,1], startangle=90,
                                          autopct='%.1f%%', fontsize=13, labels=['사망','생존'])
# ax[1,1].set(title = 'Survived - pie', ylabel = '')

 

 

 

 

 

 2/   figure, ax를 설정한 뒤 해당 자리에 그림 그리기

 

plt.figure(figsize=(7,4)) #도화지

plt.subplot(2,2,1)
train['Survived'].value_counts().plot.bar()

plt.subplot(2,2,4)
train['Survived'].value_counts().plot(kind='bar') # 위와 같은 그래프.