【1x00】方法描述
matplotlib.pyplot.pie() 方法用于绘制饼状图。
基本语法:
matplotlib.pyplot.pie( x[, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, \*, data=None] )
【2x00】简单示例
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] x = [10, 30, 45, 15] labels = ['Java', 'Golang', 'Python', 'C++'] colors = ['red', 'yellow', 'blue', 'green'] # 指定4个扇区所占比例以及扇区的颜色,扇区文本标签距离扇区中心1.1 plt.pie(x, labels=labels, colors=colors, labeldistance=1.1) plt.title('饼状图简单示例') plt.show()
运行结果:
【3x00】按角度调整扇形标签
rotatelabels 属性可以设置是否按照角度调整每块饼的 label(标签)显示方式。
i
mport matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] x = [10, 30, 45, 15] labels = ['Java', 'Go', 'Python', 'C++'] colors = ['red', 'yellow', 'blue', 'green'] # 指定4个扇区所占比例以及扇区的颜色,扇区文本标签距离扇区中心1.1,按角度调整 labels plt.pie(x, labels=labels, colors=colors, labeldistance=1.1, rotatelabels=True) plt.title('饼状图按角度调整 labels 示例') plt.show()
运行结果:
【4x00】显示图例
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] x = [10, 30, 45, 15] labels = ['Java', 'Go', 'Python', 'C++'] colors = ['red', 'yellow', 'blue', 'green'] plt.pie(x, labels=labels, colors=colors, labeldistance=1.1) plt.title('饼状图显示图例示例') plt.legend(bbox_to_anchor=(1, 1)) plt.show()
运行结果:
更多Python知识,请关注Python视频教程!!
相关推荐: