我有一个简单的 Python 饼图:values = [3, 5, 12, 8]
labels = ['a', 'b', 'c', 'd']
plt.pie(values, labels)看起来像:我还有一个值字典:dictionary = {'a': 0.31, 'b': 0.11, 'c' : 0.07, 'd': 0.12}我想用字典中对应的值来标记每个切片。我怎么做?我读了这篇文章,它演示了如何向autopct函数传递额外的参数,但似乎每个切片的参数必须相同,而在这种情况下,每个切片的参数都不同。
1 回答
![?](http://img1.sycdn.imooc.com/54584f3100019e9702200220-100-100.jpg)
繁花不似锦
TA贡献1851条经验 获得超4个赞
我知道您正在寻找的是用字典中定义的值来标记每块饼的份额,对吗?
就像是:
dictionary = {'a': 0.31, 'b': 0.11, 'c' : 0.07, 'd': 0.12}
labels = dictionary.keys()
sizes = dictionary.values()
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
plt.show()
希望这对你有用。
添加回答
举报
0/150
提交
取消