饼状图用来显示部分在总体中的占比。
基础的饼图
#饼形图
fruit = ['苹果','橘子','香蕉','芒果']
rate = [15,25,30,30]
plt.pie(rate,labels=fruit)
plt.show()
输出
饼图的常用属性
explode:每一部分突出显示距离原来的距离。
autopct:将每一块所占的比例以函数或是格式化字符串来显示,autopct='%1.1f%%'保留一位小数。
shadow:是否显示阴影,默认为False不显示。
import matplotlib.pyplot as plt
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
输出
官网可以看到更多的饼图输出样式:https://matplotlib.org/gallery/pie_and_polar_charts/pie_features.html#sphx-glr-gallery-pie-and-polar-charts-pie-features-py
网友评论