我正在处理财务数据,我打算弄清楚如何在数据上创建嵌套的饼图。具体来说,我过滤了导出和导入产品数据并为其渲染嵌套图。我确实为每个渲染了饼图,但是我无法为数据获取正确的嵌套饼图或甜甜圈图。我调查了['Bradford', 'Birkenhead', 'Bootle', 'Bury', 'Chelmsford', 'Chesterfield', 'Glasgow', 'Cumbernauld', 'London', 'Coventry', 'Dundee', 'Durham', 'East Kilbride', 'Glasgow', 'Harlow', 'Hartlepool', 'Liverpool', 'Maidstone', 'Middlesbrough', 'Newcastle upon Tyne', 'Nuneaton', 'Oldham', 'Preston', 'Sheffield', 'St Helens', 'Widnes']
上可能发布的信息,但没有找到如何获取情节的任何线索。
我当前的输出:
SO
当前情节:
运行上述解决方案后,我得到了这个图:
所需的情节
实际上我想使用相同的数据呈现此饼图或甜甜圈图:
如何获得该图?有什么技巧可以做到这一点?谢谢
答案 0 :(得分:2)
我只是编写了最少的代码来实现您想要的目标:
import matplotlib.pyplot as plt
import numpy as np
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
lbls = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
# Intended to serve something like a global variable
class MyClass:
i = -1
def func(pct, labels, vals):
MyClass.i +=1
# Returns absolute value against the default percentage
# absolute = int(pct/100.*np.sum(vals))
# Combine labels and values
return "{:s}\n{:.0f} %".format(labels[MyClass.i], pct)
fig1, ax1 = plt.subplots()
# Pie wedgeprops with width being the donut thickness
ax1.pie(sizes, wedgeprops=dict(width=0.7), autopct=lambda pct: func(pct, lbls, sizes),
shadow=True, startangle=90)
sumstr = 'Total = '+str(np.sum(sizes))
# String on the donut center
ax1.text(0., 0., sumstr, horizontalalignment='center', verticalalignment='center')
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
这将产生以下图表: