Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double donut chart in 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?

enter image description here

like image 893
clstaudt Avatar asked May 24 '17 08:05

clstaudt


1 Answers

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

enter image description here

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

like image 78
ImportanceOfBeingErnest Avatar answered Oct 07 '22 23:10

ImportanceOfBeingErnest