📚파이썬 머신러닝 판다스 데이터분석 part4. 데이터 시각화

📄part4 matplotlib.ipynb

🐿️ 데이터 불러오기 (선 그래프에 이어서)

df=pd.read_excel('data/시도별 전출입 인구수.xlsx', header=0)
df.fillna(method='ffill',inplace=True)
mask=(df['전출지별']=='서울특별시')&(df['전입지별']!='서울특별시')
df_seoul=df[mask]
df_seoul.drop(['전출지별'],axis=1, inplace=True)
df_seoul.rename({'전입지별':'전입지'},axis=1,inplace=True)
df_seoul.set_index('전입지', inplace=True)

🐿️막대 그래프

막대 그래프(bar plot)는 데이터 값의 크기에 비례하여 높이를 갖는 직사각형 막대로 표현함 막대 높이의 상대적 길이 차이를 통해 값의 크고 작음을 설명함

col_years=list(map(str, range(2010, 2018)))
df_4=df_seoul.loc[['충청남도','경상북도','강원도','전라남도'], col_years]
df_4=df_4.transpose()

# 스타일 서식 지정
plt.style.use('ggplot')

# 데이터프레임의 인덱스를 정수형으로 변경 (x축 눈금 라벨 표시)
df_4.index=df_4.index.map(int)

# 면적그래프 그리기
df_4.plot(kind='bar', figsize=(20,10), width=0.7, color=['orange', 'green','skyblue','blue'])

plt.title('서울 -> 타시도 인구 이동', size=30)
plt.ylabel('이동 인구 수', size=20)
plt.xlabel('기간', size=20)
plt.ylim(5000, 30000)
plt.legend(loc='best', fontsize=15)

plt.show()

Untitled

🌵가로형 막대 그래프

가로형 막대 그래프는 각 변수 사이 값의 크기 차이를 설명하는데 적합 plot() 메소드의 옵션으로 kind='barh' 로 입력

col_years=list(map(str, range(2010, 2018)))
df_4=df_seoul.loc[['충청남도','경상북도','강원도','전라남도'], col_years]

# 2010~2017년 이동 인구 수를 합계하여 새로운 열 추가
df_4['합계']=df_4.sum(axis=1)

# 가장 큰 값부터 정렬
df_total=df_4[['합계']].sort_values(by='합계', ascending=True)

# 스타일 서식 지정
plt.style.use('ggplot')

# 수평 막대 그래프 그리기
df_total.plot(kind='barh', figsize=(10, 5), width=0.5, color='cornflowerblue')

plt.title('서울 -> 타시도 인구 이동', size=20)
plt.xlabel('이동 인구 수', size=10)
plt.ylabel('전입지', size=10)

plt.show()

Untitled