새로새록

pandas 시각화 - 기본/seaborn를 이용 본문

소프트웨어융합/코드잇 정리.py

pandas 시각화 - 기본/seaborn를 이용

류지나 2021. 7. 22. 14:37
  • df['a'].plot(kind='bar') #막대
  • df['a'].plot(kind='pie') #파이

adobe = (df['company'] == 'Adobe') & (df['race'] == 'Overall_totals')& (df['count'] != 0)
except_total = (df['job_category'] != 'Totals') & (df['job_category'] != 'Previous_totals')
abobe_job = df[adobe & except_total]

abobe_job.set_index('job_category', inplace=True)
abobe_job.plot(kind='pie', y='count')

  • df.plot(kind='hist', y='Height', bins=15) #히스토그램
  • df.plot(kind='box', y='math score', 'english score') #박스
  • df.plot(kind='scatter', x='math score', y='reading score') #산점도

#df[['math score','writing score']].corr() #상관계수 -1~1. 절댓값1에 가까울수록 유사도상승


  • seaborn 이용 #!pip install seaborn == 0.9.0 #import seaborn as sns 

 

  • 히스토그램과 pdf: 확률 밀도함수 --> kdeplot : kernel density estimation 

ex.1

  • body_df['Height'].value_counts().sort_index().plot()
  • sns.kdeplot(body_df["Height"], bw=0.5) #bw로 모양조절.

ex2.

  • body_df.plot(kind='hist', y='Height', bins=15)
  • sns.distplot(body_df['Height'], bins=15) #bins로 위 그래프+pdf 함수라고 보면됨.

 

  • 산점도와 등고선

ex.

  • body_df.plot(kind='scatter', x='Height', y='Weight')
  • sns.kdeplot(body_df[Height'], body_df['Weight'])

 

  • LM PLOT

ex.

  • body_df.plot(kind='scatter', x='Height', y='Weight')
  • sns.lmplot(data=body_df, x='Height', y='Weight') #회귀선

 

  • voilin plot (boxplot과 kdeplot을 동시에)

ex.

  • body_df.plot(kind='box', y='Height')
  • sns.violinplot(y=body_df['Height']

아래와 같이 kdeplot에서 알수 없는 하한, 상한, 1q, 3q, median 등을 가시적으로 확인 가능

  • 단일 category가 있는 경우 --> 수평의 그래프

ax = sns.violinplot(x=tips["total_bill"])

  • x 값에 여러 개의 범주가 있는 경우 --> 수직의 그래프

ax = sns.violinplot(x="day", y="total_bill", data=tips)

그리고, 여러개의 범주 내부에서 또 값들을 나눠서 보고 싶다면, hue 파라미터를 통해 설정합니당

ax = sns.violinplot(x="day", y="total_bill", hue="smoker", data=tips, palette="muted", split=True)