Home > Enterprise >  How to equalise gap width for an exploded pie chart in matplotlib
How to equalise gap width for an exploded pie chart in matplotlib

Time:07-20

Right. I have a pie chart broadly similar to example pie chart demonstrating the problem

The issue I've got is that the white spaces between segments are not equal, they seem to be proportional to wedge size. Is there a way of setting these to all be the same width? I understood the explode function to be a proportion of the whole pie radius, so surely they ought to be the same?

CodePudding user response:

The explode parameter is the fraction of a radius by which the respective chunk will move up or down in the circle. If you shift by the same parameter the initial distance won't be kept as long as the chunks aren't of the same size. Even though it is possible to specify the radius, it doesn't seems to me trivial to find individual explode values so that the distance will be equal.

My alternative suggestion is the following one where we can create a "fictive" distance with the border of the chunks:

import matplotlib.pyplot as plt

sizes = [1, 1.2, 1.4, 1.7, 2.0, 2.4, 2.9, 3.5, 4.2, 5.1,]
wp = {'linewidth': 0.5, 'edgecolor':'white'}

fig1, ax1 = plt.subplots()
ax1.pie(sizes, 
        wedgeprops=wp)

plt.show()

The pie chart would look like that:

enter image description here

CodePudding user response:

Yes, you can but you will have to calculate a different radius for each slice. The gaps will be the same but the slices will be pulled out a different amount.

The gap a slice makes by itself is r*sin(theta/2) where r is the radial displacement and theta is the angle swept by the slice. From this you can back-calculate the r of the slice for a given gap/2.

While you will get the gaps equal; small slices will radially move a lot more than large slices, though possibly not the look you are after.

  • Related