经常遇到画图展示的问题,简单写个函数,做个备注;
Talk is cheap,show me the code!
def pie_chart():
import matplotlib.pyplot as plt
import numpy as np
#输入要展示的数据,计算和以及对应的比例
Num = [[5, 3, 14], [13, 10, 7]]
total = np.array(Num)
sum_value = np.sum(total)
size = np.sum(total,axis=0)
percent = size/sum_value
##设置要展示的内容,一共三块内容;
labels = [f"resolved: {size[0]},percent:{format(percent[0],'.2%')}", f"unresolved:{size[1]}\npercent:{format(percent[1],'.2%')}", f"unsupported:{size[2]},percent:{format(percent[2],'.2%')}"]
#三个部分对应的颜色color,以及凸出的程度;
colors = ['red', 'green', 'blue']
expodes = (0.1, 0, 0)
# 设置标题以及绘图的属性
plt.title('badcase distribution', fontsize=20, color='purple')
plt.pie(size, explode=expodes, labels=labels,shadow=True,colors=colors)
plt.gcf().subplots_adjust(left=0.1)
# 保存并显示
plt.savefig('pie.jpg')
plt.show()
结果如下:
有问题欢迎随时交流!