본문 바로가기
Pyhton/matplotlib

Day38_Python(14)_Pm

by roaring90s 2025. 10. 2.

dataFramePlotEx1

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

np.random.seed(777)
frame = pd.DataFrame(np.random.rand(5,3),
                     index=['customer1','customer2','customer3','customer4','customer5'],
                     columns=['metric1','metric2','metric3'])
print(frame)

fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,7))
frame.plot(kind='bar', ax=ax1, alpha=0.75, title='bar plot')
ax1.set_xlabel('customer')
ax1.set_ylabel('value')
plt.setp(ax1.get_xticklabels(),rotation=45)

colors = dict(boxes='blue',whiskers='orange', medians='red', caps='k')
frame.plot(kind='box', ax=ax2, title='box plot', color=colors)
ax2.set_xlabel('metrics')
ax2.set_ylabel('value2')

plt.show()

 


 

 


 

seabornEx

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

plt.rc('font', family='Malgun Gothic')

welfare = pd.read_csv('welfareClean.csv', encoding='cp949')

welfare.info()
print(welfare.tail(10))

result = welfare.groupby('결혼 유무')['결혼 유무'].count()
print(result)
plt.bar(result.index, result.values)
plt.show()

#-------------- seaborn 사용시 더 편하게 출력 가능-------------------
# sns.countplot(data=welfare, x='결혼 유무', order=['결혼','이혼','무응답'])
# sns.countplot(data=welfare, x='결혼 유무', order=['결혼','이혼','무응답'], hue='종교 유무')
sns.countplot(data=welfare, x='결혼 유무', order=['결혼','이혼','무응답'], hue='종교 유무', palette='Paired')
plt.title('count plot', fontsize=18)
plt.show()

 


pdata = welfare.pivot_table(index='성별',
                            columns='결혼 유무',
                            values='나이',
                            aggfunc='mean')
print(pdata)
sns.heatmap(data=pdata, annot=True)         #annot = 그래프 안에 값을 보여줌
#데이터의 전반적인 패턴을 보고 싶을때 쓰는것
plt.show()

 

 

corr = welfare[['생일','소득','나이']].corr()
print(corr)
sns.heatmap(data=corr, annot=True, cmap='YlGnBu')
plt.show()

 

nwelfare = welfare.loc[:,['생일','소득','나이']]
print(nwelfare)
sns.pairplot(data=nwelfare)
plt.show()

 

nwelfare = welfare.loc[:,['생일','소득','나이','결혼 유무']]
print(nwelfare)
sns.pairplot(data=nwelfare, hue='결혼 유무')
plt.show()

 

sns.violinplot(x='성별', y='나이', data=welfare, hue='종교 유무')
plt.show()

 

sns.lmplot(x='나이', y='소득', data=welfare, hue='결혼 유무')
#리니어 모델
plt.show()

 

sns.lmplot(x='나이', y='소득', data=welfare, hue='결혼 유무',
           col='성별', row='연령대')
plt.show()

 

 


여러가지 그래프들이 많이 있다.

 


 

'Pyhton > matplotlib' 카테고리의 다른 글

Day38_Python(14)_Am  (0) 2025.10.02
Day37_Python(13)_Pm  (0) 2025.10.01
Day37_Python(13)_Am  (0) 2025.10.01
Day36_Python(12)_Pm  (0) 2025.09.30
Day36_Python(12)_Am  (0) 2025.09.30