作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使用Python的matplotlib为圆环饼图制作子图?
下面是我编写的代码:
import matplotlib.pyplot as plt
labels = 'Frogs', 'Hogs', 'Dogs'
sizes = [15, 30, 45]
colors = ['yellowgreen', 'gold', 'lightskyblue']
explode = (0, 0, 0) # explode a slice if required
subgroup_names = ['A.1', 'A.2', 'A.3', 'B.1', 'B.2', 'C.1']
subgroup_size = [13, 3, 5, 6, 5, 10]
a, b, c = [plt.cm.Blues, plt.cm.Reds, plt.cm.Greens]
plt.pie(sizes, explode=explode, labels=labels, pctdistance=0.45, labeldistance=0.65, colors=colors, autopct='%.2f%%', shadow=True)
centre_circle = plt.Circle((0, 0), 0.175, color='black', fc='white', linewidth=1.25)
plt.pie(subgroup_size, labels=subgroup_names, pctdistance=0.85, labeldistance=1.25, colors=[a(0.5), a(0.4), a(0.3), b(0.5), b(0.4), c(0.6)], autopct='%.2f%%', shadow=True)
centre_circle2 = plt.Circle((0, 0), 0.275, color='red', fc='white', linewidth=1.25)
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
fig.gca().add_artist(centre_circle2)
plt.axis('equal')
plt.show()
结果如下图所示:
但我希望结果与下图类似:
(我必须显示百分比值)
最佳答案
Matplotlib 文档提供了非常好的示例,几乎涵盖了您能想到的所有内容。请参阅here用于嵌套饼图。
在对仅链接答案的评论之后,关键字 radius 是您在调用 pie() 时要查找的内容。它接受一个 float ,决定饼图的绝对半径。因此,绘制嵌套饼图需要多次调用pie(),每个调用具有不同的半径值。请参阅下面的示例
import matplotlib.pyplot as plt
plt.pie([1, 2, 3], radius=1,
colors=['xkcd:cerulean', 'xkcd:bright red', 'xkcd:grass green'],
labels=['Group A', 'Group B', 'Group C'], autopct='%.2f%%',
pctdistance=0.85, shadow=True,
wedgeprops=dict(width=0.3, edgecolor='white'))
plt.pie([0.3, 0.7, 0.4, 1.5, 0.1, 3], radius=0.7,
colors=['xkcd:sky blue', 'xkcd:light blue', 'xkcd:reddish pink',
'xkcd:salmon pink', 'xkcd:baby pink', 'xkcd:apple green'],
wedgeprops=dict(width=0.3, edgecolor='white'),
labels=['A.1', 'A.2', 'B.1', 'B.2', 'B.3', 'C.1'], autopct='%.2f%%',
pctdistance=0.8, labeldistance=0.4, shadow=True)
plt.axis('equal')
plt.show()
关于python - 如何使用 matplotlib 在圆环饼图中制作子图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53293382/
有人可以帮我理解为什么我的饼图百分比计算不正确吗?看截图: 根据我的计算,如 RHS 上所示,支出百分比应为 24.73%。传递给 Highcharts 的值如下:- 花费:204827099.36-
我正在制作圆环饼图。 我设置数据的颜色并获取钻取(它是保存外部切片的数据和配置的对象)切片的颜色为同一组颜色。我想设置要在向下钻取对象中使用的不同颜色数组。请参阅附图(外层切片有两种颜色:橙色和透明)
我是一名优秀的程序员,十分优秀!