データ分析と可視化
25921 ワード
13.円図(デフォルト)
14.円図(深化)
14-1. DataFrameの利用
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family'] = 'Malgun Gothic' #windows
matplotlib.rcParams['font.size'] = 15
matplotlib.rcParams['axes.unicode_minus'] = False
values = [30, 25, 20, 13, 10, 2]
labels = ['Python', 'Java', 'Javascript', "C#", 'C/C++', 'ETC']
plt.pie(values, labels=labels, autopct = '%.1f%%', startangle=90, counterclock=False)
#퍼센트를 기준으로 나눠서 나타냄
#%%를 두번써주면 뒤에 %까지 붙음
#startangle은 기준을 중간 90도부터 시계반대방향으로
#시계방향으로 설정하고 싶으면 counterclock사용
plt.show()

values = [30, 25, 20, 13, 10, 2]
labels = ['Python', 'Java', 'Javascript', "C#", 'C/C++', 'ETC']
#explode = [0.2, 0.1, 0, 0, 0, 0] #간격띄우기
explode = [0.05]*6 #동일간격 띄우기
plt.pie(values, labels=labels, explode = explode)
plt.show()

plt.pie(values, labels=labels, explode = explode)
plt.legend(loc=(1.2, 0.3))
plt.show()

plt.pie(values, labels=labels, explode = explode)
plt.title('언어별 선호도')
plt.legend(loc=(1.2, 0.3), title='언어')
plt.show()

14.円図(深化)
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family'] = 'Malgun Gothic' #windows
matplotlib.rcParams['font.size'] = 15
matplotlib.rcParams['axes.unicode_minus'] = False
values = [30, 25, 20, 13, 10, 2]
labels = ['Python', 'Java', 'Javascript', "C#", 'C/C++', 'ETC']
#colors = ['b', 'g', 'r','c', 'm', 'y']
colors = ['#ffadad', '#ffd6a5', '#fdffb6', '#caffbf', '#9bf6ff', '#a0c4ff']
explode = [0.05]*6
plt.pie(values, labels=labels, autopct = '%.1f%%', startangle=90, counterclock=False, colors= colors, explode = explode)
plt.show()

wedgeprops = {'width':0.6}
plt.pie(values, labels=labels, autopct = '%.1f%%', startangle=90, counterclock=False, colors= colors, explode = explode, wedgeprops=wedgeprops)
plt.show()

wedgeprops = {'width':0.6, 'edgecolor':'w', 'linewidth':3}
plt.pie(values, labels=labels, autopct = '%.1f%%', startangle=90, counterclock=False, colors= colors, wedgeprops=wedgeprops)
plt.show()

def custom_autopct(pct):
return ('%.1f%%' % pct) if pct >= 10 else ''
plt.pie(values, labels=labels, autopct = custom_autopct, startangle=90, counterclock=False, colors= colors, wedgeprops=wedgeprops)
plt.show()

#방법2
def custom_autopct(pct):
return '{:.1f}%'.format(pct) if pct >=10 else ''
plt.pie(values, labels=labels, autopct = custom_autopct, startangle=90, counterclock=False, colors= colors, wedgeprops=wedgeprops)
plt.show()

# 정수부분만 출력
def custom_autopct(pct):
return '{:.0f}%'.format(pct) if pct >=10 else ''
plt.pie(values, labels=labels, autopct = custom_autopct, startangle=90, counterclock=False, colors= colors, wedgeprops=wedgeprops)
plt.show()

#숫자 거리띄우기
def custom_autopct(pct):
return '{:.0f}%'.format(pct) if pct >=10 else ''
plt.pie(values, labels=labels, autopct = custom_autopct, startangle=90, counterclock=False, colors= colors, wedgeprops=wedgeprops, pctdistance=0.7)
plt.show()

14-1. DataFrameの利用
import pandas as pd
df = pd.read_excel('../Pandas/score3.xlsx')
df

#학교대비 학생수
grp = df.groupby('학교')
grp
>>>
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x00000205FA4755E0>
grp.size()

grp.size()['북산고']
>>> 5
grp.size()['능남고']
>>> 3
values = [grp.size()['북산고'], grp.size()['능남고']] #[5,3]
labels =['북산고', '능남고']
plt.pie(values, labels=labels)
plt.title('소속학교')
plt.show()

Reference
この問題について(データ分析と可視化), 我々は、より多くの情報をここで見つけました https://velog.io/@ssjy89/나도코딩-데이터-분석-및-시각화-Matplotlib6テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol