2016-11-11 98 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