matplotlib 中的双圆环图 [英] Double donut chart in matplotlib

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

问题描述

好的 matplotlib 爱好者,我们知道如何绘制

解决方案

要获得双圆环图,您可以根据需要在同一图中绘制任意数量的饼图.因此,外饼的 width 设置为其楔形,而内饼的半径小于或等于 1-width.

将 matplotlib.pyplot 导入为 plt将 numpy 导入为 np图, ax = plt.subplots()ax.axis('相等')宽度 = 0.3cm = plt.get_cmap("tab20c")cout = cm(np.arange(3)*4)饼,_ = ax.pie([120,77,39],半径=1,标签=列表(ABC"),颜色=cout)plt.setp( pie, width=width, edgecolor='white')cin = cm(np.array([1,2,5,6,9,10]))标签 = 列表(地图(".join,zip(列表(aabbcc"),地图(str,[1,2]*3))))pie2, _ = ax.pie([60,60,37,40,29,10],radius=1-width,labels=labels,标签距离=0.7,颜色=cin)plt.setp( pie2, width=width, edgecolor='white')plt.show()

注意:我在 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?

解决方案

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

Note: I made this code also available in the matplotlib gallery as nested pie example.

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

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