如何使matplotlib AutoCT成为标签名称的函数?

2024-09-30 14:29:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的Python饼图:

values = [3, 5, 12, 8]
labels = ['a', 'b', 'c', 'd']
plt.pie(values, labels)

看起来像:

pie chart

我还有一本价值观词典:

dictionary = {'a': 0.31, 'b': 0.11, 'c' : 0.07, 'd': 0.12}

我想在字典中给每个片段加上相应的值。我该怎么做?我阅读了this post,它演示了如何向autopct函数传递额外的参数,但似乎每个片段的参数必须相同,而在本例中,每个片段的参数不同


Tags: 函数参数labelsdictionary字典pltthispost
1条回答
网友
1楼 · 发布于 2024-09-30 14:29:58

我知道你要找的是在每一块饼上都贴上标签,在dict中定义为值,对吗

比如:

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()

希望这对你有用

相关问题 更多 >