Skip to main content
 首页 » 编程设计

python之饼图切片 python 上的行值

2024年11月24日13youxin

我有以下数据集:

import matplotlib.pyplot as plotter 
 
    pieLabels              = 'Asia', 'Africa', 'Europe', 'North America', 'South America', 'Australia' 
    populationShare     = [5.69, 16, 9.94, 7.79, 5.68, 6.54] 

做馅饼:

figureObject, axesObject = plotter.subplots() 
axesObject.pie(populationShare, 
        labels=pieLabels, 
        startangle=90) 
axesObject.axis('equal') 
plotter.show() 

我想制作一个饼图,并将 populationShare 的实际值打印在饼图的切片上。不是百分比。

请您参考如下方法:

尝试如下:

import matplotlib.pyplot as plotter 
 
pieLabels  = ['Asia', 'Africa', 'Europe', 'North America', 'South America', 'Australia'] 
populationShare = [5.69, 16, 9.94, 7.79, 5.68, 6.54] 
 
figureObject, axesObject = plotter.subplots() 
axesObject.pie(populationShare, 
               labels=pieLabels, 
               autopct=lambda p: '{:.2f}'.format(p * sum(populationShare) / 100), 
               startangle=90) 
axesObject.axis('equal') 
plotter.show()