개발/파이썬

파이썬. Mabplotlib - Pie Charts

웅'jk 2022. 11. 28. 14:20

이번에는 둥그런 모양의 차트로 만들어 봅시다

 

앞선 포스팅과 마찬가지로 df 에는 다음과 같은 데이터 값이 들어있습니다.

먼저 파이차트는 데이터프레임 자체로는 바로 구할 수가 없기 때문에

먼저 나타낼 데이터값들을 구해놓아야 합니다. (Pandas Series)

df2 = df['generation_id'].value_counts()
# df 로는 바로 구할 수 없어 만든 df2 = df의 generation_id 컬럼 값

plt.pie(df2,labels= df2.index, autopct='%.1f', startangle=90, 
       wedgeprops= {'width' : 0.7} )
# labels= 은 각 데이터별 인덱스값, autopct= 데이터를 &표시 , startangle= 데이터의 시작위치
# wedgeprops = 중앙의 여백
plt.title('generation id pie chart')
# 차트의 title 생성
plt.legend(['id 5','id 1','id 3','id 4','id 2','id 7','id 6'])
# 각 차트가 무엇을 의미하는지 나타내는 표
plt.show()

파이 차트는 데이터값으로 pandas series 값을 받기때문에 정렬을 위한 order 가아닌

입력할 데이터가 정렬이 되어 있어야 한다.

df3 = df2[0:4]
plt.pie(df3,labels= df3.index, autopct='%.1f', startangle=90, 
       wedgeprops= {'width' : 0.7} )
plt.title('generation id pie chart')
plt.show()