Home > Net >  How to remove white space in the middle of a pie chart?
How to remove white space in the middle of a pie chart?

Time:12-14

Below is the example code I made to have a reproducible example.

import numpy as np    
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

size = 0.3
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])

cmap = plt.get_cmap("tab20c")
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap([1, 2, 5, 6, 9, 10])

ax.pie(vals.sum(axis=1), radius=3-0.9, colors=outer_colors,
       wedgeprops=dict(width=0.9, edgecolor='w'),labels=["Europe","North America","Scandiavia"],
       pctdistance=1.1, labeldistance=0.65)

ax.pie(vals.flatten(), radius=3, colors=inner_colors,
       wedgeprops=dict(width=0.9, edgecolor='w'),labels=["Germany","France","USA","Mexico","Finland","Sweden"],
       pctdistance=1.1, labeldistance=0.85)

ax.set(aspect="equal", title='')
plt.show()

which gives the following chart :

enter image description here

How can we play with the radius to reduce or even delete the white space in the middle ?

the goal is to have the following :

enter image description here

CodePudding user response:

Try out following code:

ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
       wedgeprops=dict(width=1, edgecolor='w'),labels=["Europe","North America","Scandiavia"],
       pctdistance=1.1, labeldistance=0.65)

ax.pie(vals.flatten(), radius=2, colors=inner_colors,
       wedgeprops=dict(width=1, edgecolor='w'),labels=["Germany","France","USA","Mexico","Finland","Sweden"],
       pctdistance=1.1, labeldistance=0.85)

It gives me this output: enter image description here

  • Related