Python中的金字塔类型图

123*_*023 0 python matplotlib

我有一个问题,我需要在python的金字塔结构中显示三个图形,如下所示:

   graph1
graph2 graph3
Run Code Online (Sandbox Code Playgroud)

我希望所有三个图表大小相同,我该怎么做?

问候

码:

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)
Run Code Online (Sandbox Code Playgroud)

然后我有三种不同的"尺寸",size_1,sizes_21和sizes_22.我的计划是三次做这些饼图.

Dav*_*idG 7

这可以通过使用matplotlibs subplot2grid函数实现,文档可以在这里找到.

下面是一个例子,其基础知识在这里找到.

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()
Run Code Online (Sandbox Code Playgroud)

这将生成以下图表:

在此输入图像描述