import matplotlib.pyplot as plt data = selected_goods['child_percent'] labels = selected_goods['Goods'] # 设置画布大小 plt.figure(figsize = (8,6)) # 设置每一块分割出的间隙大小 explode = (0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.3,0.1,0.3) plt.pie(data,explode = explode,labels = labels,autopct = '%1.2f%%', pctdistance = 1.1,labeldistance = 1.2) # 设置标题 plt.title("Percentage of sales by item within non-alcoholic beverages") plt.axis('equal') # 保存图形 plt.savefig('child_persent.png') # 展示图形 plt.show()标解注释
时间: 2024-04-03 14:34:26 浏览: 54
matlab设置画布大小代码-pyplot-tutorial:代码示例了解用于绘制图形的matplotlib.pyplot接口
这段代码使用 Matplotlib 绘制了一个饼图,表示非酒精饮料内部各商品销售量占比。具体解释如下:
1. `data = selected_goods['child_percent']` 和 `labels = selected_goods['Goods']` 分别取出每个商品销售量占总销售量的百分比和商品名称,用于绘制饼图。
2. `plt.figure(figsize = (8,6))` 设置画布的大小为 8x6。
3. `explode = (0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.3,0.1,0.3)` 设置每一块分割出的间隙大小,使得某些块分离出来,突出显示。
4. `plt.pie(data,explode = explode,labels = labels,autopct = '%1.2f%%', pctdistance = 1.1,labeldistance = 1.2)` 绘制饼图,其中 `data` 是每个商品销售量占总销售量的百分比,`explode` 是每一块分割出的间隙大小,`labels` 是每个商品的名称,`autopct` 是设置每一块的文本标签格式,`pctdistance` 是设置每一块的文本标签距离圆心的距离,`labeldistance` 是设置每个标签距离饼图的距离。
5. `plt.title("Percentage of sales by item within non-alcoholic beverages")` 设置饼图的标题。
6. `plt.axis('equal')` 将 x 轴和 y 轴的比例设置为相等的,使得饼图呈圆形。
7. `plt.savefig('child_persent.png')` 将饼图保存为一个 PNG 文件。
8. `plt.show()` 展示饼图。
阅读全文