Jens grønborg Jens grønborg - 4 months ago 36
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:

graph2 graph3

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



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

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


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

This produces the following graph:

enter image description here