Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Right. I have a pie chart broadly similar to this example using this code:

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,]
explode = [0.05]*len(sizes)
wp = {'linewidth': 1, 'edgecolor':'black'}

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

plt.show()

it results in an image like this:

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?

like image 740
Will Avatar asked Jun 07 '26 20:06

Will


1 Answers

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

like image 133
Kateryna Avatar answered Jun 10 '26 10:06

Kateryna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!