pyplot.pie() で円グラフを作成する
# sample code 10-1
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
values = [10, 20, 25, 30, 50]
labels = ['Apple', 'Banana', 'Grape', 'Oange', 'Pineapple']
fig, ax = plt.subplots(1,1,figsize=(6,4))
ax.pie(
x=values,
labels=labels,
autopct='%.2f%%' # 小数点以下2桁まで表示
)
ax.axis('equal') # 楕円にならないように
plt.show()
# sample code 10-2
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
values = [10, 20, 25, 30, 50]
labels = ['Apple', 'Banana', 'Grape', 'Oange', 'Pineapple']
fig, ax = plt.subplots(1,1,figsize=(6,4))
ax.pie(
x=values,
labels=labels,
autopct='%.2f%%',
startangle=90, # 90°(12時)の位置から開始
counterclock=False # 時計回り
)
ax.axis('equal') # 楕円にならないように
plt.show()
# sample code 10-3
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
values = [10, 20, 25, 30, 50]
labels = ['Apple', 'Banana', 'Grape', 'Oange', 'Pineapple']
colors = ['red', 'violet', 'deeppink', 'fuchsia', 'deeppink', 'pink']
fig, ax = plt.subplots(1, 1, figsize=(6,4))
ax.pie(
x=values,
labels=labels,
colors=colors,
wedgeprops={
'linewidth': 3,
'edgecolor':'white'
},
labeldistance=0.5,
textprops={
'color':'white',
'weight':'bold'
}
)
plt.axis('equal')
plt.show()
# sample code 10-4
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
values = [10, 20, 25, 30, 50]
labels = ['Apple', 'Banana', 'Grape', 'Oange', 'Pineapple']
explodes = [0.4, 0, 0, 0.1, 0]
fig, ax = plt.subplots(1,1,figsize=(6,4))
ax.pie(
x=values,
labels=labels,
autopct='%.2f%%',
explode=explodes
)
ax.axis('equal') # 楕円にならないように
plt.show()