matplotlib preserve aspect ratio in Wedge patches (pie charts)

I tried the same thing, and matplotlib really doesn’t try to make this easy for you, but I found a solution that you should be able to use.

You need to separate the centers from the wedges and add them to the PatchCollection as offsets. Then you can apply different transforms to the offsets (transOffset) and shape (transform).

Notice that I have changed the r-value (radius). This value is no longer in data coordinates, so it should always be the same size, regardless of how much you zoom, but it is too small to be visible at i/10.

from matplotlib import patches, collections, transforms

offsets = []
for i in range(2, 2 + N):
    thetas = np.linspace(0, 360, num=i)
    assert len(thetas) - 1 <= len(colors)
    for theta1, theta2, c in zip(thetas[:-1], thetas[1:], colors):
        wedge = patches.Wedge((0, 0), r=10, theta1=theta1, theta2=theta2,
                              color=c)
        offsets.append((i, i))
        pies.append(wedge)

coll = collections.PatchCollection(
    pies, match_original=True, offsets=offsets,
    transform=transforms.IdentityTransform(),
    transOffset=axes.transData
)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top