Reputation: 717
I am trying to recreate a donut chart where the last wedge is thinner than the rest, like this :
But I cant find a way to make the last wedge have a smaller width. So I tried having the last wedge be the same color as the backrground, and drawing a grey cicrle on top. But I cant get the cirle drawn underneath the wedges and have the above layers be transparent in the right places.
Is there a way to make a single wedge transparent, or otherwise solve my problem?
Here is my code so far:
import matplotlib.pyplot as plt
donut = [150,10,20,20,30,40]
total = sum(donut)
grey_circle = plt.Circle((0,0),0.965,color='#CCCCCC', lw=1, fill=False)
centre_circle = plt.Circle((0,0),0.93,fc='white')
fig, ax = plt.subplots(figsize=(2, 2), subplot_kw=dict(aspect="equal"))
colors = ['#F8F5EB','#50E3C2','#FF9100','#002776','#C94096','#0071CD' ]
wedges, texts = ax.pie(donut, colors=colors, wedgeprops=dict(width=0.1), startangle=90)
fig.gca().add_artist(grey_circle)
fig.gca().add_artist(centre_circle)
fig.set_facecolor('#F8F5EB')
fig = plt.gcf()
plt.savefig('cirle.png',facecolor=fig.get_facecolor(), edgecolor='none', dpi=300)
plt.show()
And my result:
Upvotes: 0
Views: 667
Reputation: 80409
The simplest way is drawing the chart twice, once for the thick wedges, once for the thin. The colors of the wedges not-to-be-drawn is set to 'none'. Setting an explicit radius will create the correct size for the thin wedges.
With this approach, the circles are not needed to hide stuff. You still can add a circle to get a different color in the centre. Just add zorder=0
to make sure the circle is behind the pie.
Some code to illustrate the concepts:
import matplotlib.pyplot as plt
donut = [150, 10, 20, 20, 30, 40]
thin_indices = [0] # indices of all wedges that should be thin
colors = ['#CCCCCC', '#50E3C2', '#FF9100', '#002776', '#C94096', '#0071CD']
colors_thin = [c if i in thin_indices else 'none' for i, c in enumerate(colors)]
colors_thick = [c if i not in thin_indices else 'none' for i, c in enumerate(colors)]
radius = 1
thick_width = 0.1
thin_width = 0.05
radius_thin = radius - 0.5 * (thick_width - thin_width)
fig, ax = plt.subplots(figsize=(2, 2), subplot_kw=dict(aspect="equal"))
ax.pie(donut, radius=radius, colors=colors_thick, wedgeprops=dict(width=thick_width), startangle=90)
ax.pie(donut, radius=radius_thin, colors=colors_thin, wedgeprops=dict(width=thin_width), startangle=90)
centre_circle = plt.Circle((0, 0), radius - thick_width/2, fc='white', zorder=0)
ax.add_artist(centre_circle)
fig.set_facecolor('#F8F5EB')
plt.savefig('cirle.png', facecolor=fig.get_facecolor(), edgecolor='none', dpi=300)
plt.show()
Upvotes: 2