matplotlib中的双甜甜圈图 [英] Double donut chart in matplotlib

查看:144
本文介绍了matplotlib中的双甜甜圈图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的matplotlib爱好者,我们知道如何绘制甜甜圈图,但是有什么比甜甜圈图?双圆环图.具体来说:我们有一组元素属于第一类的不相交的类别和子类别.甜甜圈图应具有外圈类别的切片和内圈子类别的切片,显然与外切片对齐.

Alright matplotlib afficionados, we know how to plot a donut chart, but what is better than a donut chart? A double-donut chart. Specifically: We have a set of elements that fall into disjoint categories and sub-categories of the first categorization. The donut chart should have slices for the categories in the outer ring and slices for the sub-categories in the inner ring, obviously aligned with the outer slices.

有没有提供此功能的库,还是我们需要在这里解决?

Is there any library that provides this or do we need to work this out here?

推荐答案

要获取双甜甜圈图,您可以在同一图中绘制任意数量的饼图.因此,外部饼图的楔形将设置为width,内部饼图的半径将小于或等于1-width.

To obtain a double donut chart, you can plot as many pie charts in the same plot as you want. So the outer pie would have a width set to its wedges and the inner pie would have a radius that is less or equal 1-width.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
ax.axis('equal')
width = 0.3

cm = plt.get_cmap("tab20c")
cout = cm(np.arange(3)*4)
pie, _ = ax.pie([120,77,39], radius=1, labels=list("ABC"), colors=cout)
plt.setp( pie, width=width, edgecolor='white')

cin = cm(np.array([1,2,5,6,9,10]))
labels = list(map("".join, zip(list("aabbcc"),map(str, [1,2]*3))))
pie2, _ = ax.pie([60,60,37,40,29,10], radius=1-width, labels=labels,
                                      labeldistance=0.7, colors=cin)
plt.setp( pie2, width=width, edgecolor='white')
plt.show()

注意:我在matplotlib画廊中也以嵌套饼图示例.

这篇关于matplotlib中的双甜甜圈图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆