2016-11-11 99 views
0

我有一个问题,我需要在一个金字塔结构在python显示三个图形,这样的事情:金字塔式的人物在Python

graph1 
graph2 graph3 

我想三个图大小相等的,我该怎么做呢?

问候

代码:

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) 

然后我有三个不同的 “大小”,sizes_1,sizes_21和sizes_22。我的计划是三次做这些饼图。

+1

如何图表格式化? –

+0

我不太清楚你的意思是通过格式化,但我基本上使用一些数据做python中的三个pie.plots – 1233023

+0

然后请向我们展示您用来绘制数据的代码。 – DavidG

回答

6

可以实现的一种方法是使用matplotlibs subplot2grid函数,文档可以找到here

下面是一个例子,其基础被发现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() 

这将产生以下图表:

enter image description here