Jens grønborg Jens grønborg - 29 days ago 12
Python Question

Pyramid type figure in Python

I have a problem, I need to display three graphs in a pyramid structure in python, something like this:

graph1
graph2 graph3


I'd like all three graphs to be of equal size, how do I do this?

Regards

Code:

import matplotlib.pyplot as plt
plt.pie(sizes_21,labels=labels,colors=colors,autopct='%1.1f%%')
plt.title('$i_G(t)$ = %1.1f' %gini([i*len(X[1:])**(-1) for i in sizes_1]),y=1.08)
plt.axis('equal')
plt.figure(1)


I then have three different "sizes", sizes_1, sizes_21 and sizes_22. My plan was to do these pie plots three times.

Answer

One way this can be achieved is by using matplotlibs subplot2grid function, the documentation can be found here.

Below is an example, the basics of which were found here.

import matplotlib.pyplot as plt

labels = ['Python', 'C++', 'Ruby', 'Java']
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']

fig,ax = plt.subplots()

#define the position of the axes where the pie charts will be plotted
ax1 = plt.subplot2grid((2, 2), (0, 0),colspan=2) # setting colspan=2 will
ax2 = plt.subplot2grid((2, 2), (1, 0))           # move top pie chart to the middle
ax3 = plt.subplot2grid((2, 2), (1, 1))

#plot the pie charts
ax1.pie(sizes, labels=labels, colors=colors,
        autopct='%1.1f%%', startangle=140)
ax2.pie(sizes, labels=labels, colors=colors,
        autopct='%1.1f%%', startangle=140)
ax3.pie(sizes, labels=labels, colors=colors,
        autopct='%1.1f%%', startangle=140)

ax1.axis('equal')   #to enable to pie chart to be perfectly circular
ax2.axis('equal')
ax3.axis('equal')

plt.show()

This produces the following graph:

enter image description here