PYTHON DATA 시각화 – SEABORN #1
seaborn 과 pandas 차이점¶
pandas : wide data seaborn : 정돈된 데이터
In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [2]:
employee = pd.read_csv('data/employee.csv', parse_dates=['HIRE_DATE','JOB_DATE'])
employee
Out[2]:
1. 각 부서 개수를 막대그래프고 그리기¶
1-1. seaborn countplot 그리기¶
In [4]:
fig, ax = plt.subplots(figsize=(8,6))
sns.countplot(y='DEPARTMENT', data=employee, ax=ax)
fig.savefig('c13-sns1.png', dpi=300, bbox_inches='tight')
1-2. pandas barplot 그리기¶
In [5]:
fig, ax = plt.subplots(figsize=(8,6))
(
employee['DEPARTMENT'].value_counts()
.plot.barh(ax=ax)
)
fig.savefig('c13-sns2.png', dpi=300, bbox_inches='tight')
2. 인종별 평균 급여 그리기¶
2-1. seaborn barplot 그리기¶
In [13]:
fig, ax = plt.subplots(figsize=(8,6))
sns.barplot(y='RACE', x='BASE_SALARY', data=employee, ax=ax)
fig.savefig('c13-sns3.png', dpi=300, bbox_inches='tight')
2-2. pandas barplot 그리기¶
In [15]:
fig, ax = plt.subplots(figsize=(8,6))
(
employee
.groupby('RACE', sort=False)
['BASE_SALARY'].mean()
.plot.barh(rot=0, width=.8, ax=ax)
)
ax.set_xlabel('Mean Salay')
fig.savefig('c13-sns4.png', dpi=300, bbox_inches='tight')
3. RACE 와 GENDER 별 평균급여 그리기¶
3-1 seaborn barplot 그리기¶
In [16]:
fig, ax = plt.subplots(figsize=(18,6))
sns.barplot(x='RACE',y='BASE_SALARY', hue='GENDER', ax=ax, data=employee, palette='Greys',
order=['Hispanic/Latino',
'Black or African American',
'American Indian or Alaskan Native',
'Asian/Pacific Islander', 'Others', 'White'])
fig.savefig('c13-sns5.png', dpi=300, bbox_inches='tight')
3-2 pandas barplot 그리기¶
In [23]:
fig, ax = plt.subplots(figsize=(18,6))
(
employee
.groupby(['RACE','GENDER'], sort=False)
['BASE_SALARY']
.mean()
.unstack('GENDER')
.sort_values('Female')
.plot.bar(rot=0, ax=ax, width=.8, cmap='viridis')
)
fig.savefig('c13-sns6.png', dpi=300, bbox_inches='tight')
4. RACE와 GENDER 별 급여를 상자 그림으로 그리기¶
4-1. seaborn boxplot 그리기¶
In [25]:
fig, ax = plt.subplots(figsize=(8,6))
sns.boxplot(x='GENDER', y='BASE_SALARY', data=employee, hue='RACE', palette='Greys', ax=ax)
fig.savefig('c13-sns7.png', dpi=300, bbox_inches='tight')
4-2. pandas bocplot 그리기¶
In [30]:
fig, axs = plt.subplots(1, 2, figsize=(12,6), sharey=True)
for g, ax in zip(['Female', 'Male'], axs):
(employee
.query('GENDER == @g')
.assign(RACE=lambda df_:df_.RACE.fillna('NA'))
.pivot(columns='RACE')
['BASE_SALARY']
.plot.box(ax=ax, rot=30)
)
ax.set_title(g + ' Salary')
ax.set_xlabel('')
fig.savefig('c13-sns8,png', bbox_inches='tight')
In [ ]: