0

I am trying to recreate a donut chart where the last wedge is thinner than the rest, like this :

enter image description here

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:

enter image description here

Ullsokk
  • 697
  • 2
  • 11
  • 24
  • [https://stackoverflow.com/questions/20551477/changing-line-properties-in-matplotlib-pie-chart](Here) you can find a way to access the wedges. Then, you can dinamically modify the width according to your will. – Cowflu Jan 23 '20 at 11:51

1 Answers1

2

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

resulting donut plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Fantastic, thanks a bunch! My only issue now is that the centre is the same color as my background, but it should be white. Tried putting a white circle back on top, but it just clips the wedges. Is there a way to replicate the white interior of my first image, while the outside is colored? – Ullsokk Jan 23 '20 at 13:33
  • Yes it does! Tried something similar without luck, didnt know about the zorder = 0, but that makes sense! Thank you again – Ullsokk Jan 23 '20 at 20:15