Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One figure to present multiple pie chart with different size

enter image description here

The figure above is an illustration of my purpose.

It's easy to plot pie chart in MatPlotLib.

But how to draw several pie in one figure and the size of each figure depend on the value I set.

Any advices or recommandation is appreciate!

like image 338
Han Zhengzu Avatar asked Mar 28 '17 15:03

Han Zhengzu


2 Answers

You can use subplots to place the pies into the figure. You can then use the radius argument to determine their size. As usual it helps to consult the manual.

Here is an example:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)

t = "Plot a pie chart  with different sized pies all in one figure"
X  = np.random.rand(12,4)*30
r = np.random.rand(12)*0.8+0.6

fig, axes= plt.subplots(3, 4)

for i, ax in enumerate(axes.flatten()):
    x = X[i,:]/np.sum(X[i,:])
    ax.pie(x, radius = r[i], autopct="%.1f%%", pctdistance=0.9)
    ax.set_title(t.split()[i])

plt.show()

enter image description here

like image 164
ImportanceOfBeingErnest Avatar answered Nov 15 '22 11:11

ImportanceOfBeingErnest


You can use add_axes to adjust the size of the axes for your plot. Also, there is a radius parameter in the pie function which you can use to specify the radius of the pie plot. Check the code below:

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]
fig = plt.figure()
ax1 = fig.add_axes([.1, .1, .8, .8], aspect=1)
ax1.pie(fracs, labels=labels)
ax2 = fig.add_axes([.65, .65, .3, .3], aspect=1)  # You can adjust the position and size of the axes for the pie plot
ax2.pie(fracs, labels=labels, radius=.8)  # The radius argument can also be used to adjust the size of the pie plot
plt.show()

enter image description here

like image 29
Longwen Ou Avatar answered Nov 15 '22 09:11

Longwen Ou