Python Matplotlib:绘制饼图,将楔形分解为条形图

Python Matplotlib:绘制饼图,将楔形分解为条形图,python,matplotlib,pie-chart,Python,Matplotlib,Pie Chart,我有一个饼图(示例),上面有以下fracs=[10,20,50,30]。使用matplotlib绘制此图没有问题。如何将第一个楔子(10)细分为6和4?理想情况下,我希望为20设置第二个楔子,将其细分为10、3、7。这将显示为特定楔块附近的条形图或饼图(这将使其成为饼图中的饼图,类似于Excel中的饼图)。以下是一种方法(可能不是最好的…)。我已经修改了一些代码,使之成为一个little_pie函数,可以在任意位置绘制小饼图 from pylab import * import math imp

我有一个饼图(示例),上面有以下
fracs=[10,20,50,30]
。使用matplotlib绘制此图没有问题。如何将第一个楔子(10)细分为
6
4
?理想情况下,我希望为
20
设置第二个楔子,将其细分为
10
3
7
。这将显示为特定楔块附近的条形图或饼图(这将使其成为饼图中的饼图,类似于Excel中的饼图)。

以下是一种方法(可能不是最好的…)。我已经修改了一些代码,使之成为一个
little_pie
函数,可以在任意位置绘制小饼图

from pylab import *
import math
import numpy as np

def little_pie(breakdown,location,size):
    breakdown = [0] + list(np.cumsum(breakdown)* 1.0 / sum(breakdown))
    for i in xrange(len(breakdown)-1):
        x = [0] + np.cos(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi *    
                          breakdown[i+1], 20)).tolist()
        y = [0] + np.sin(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi * 
                          breakdown[i+1], 20)).tolist()
        xy = zip(x,y)
        scatter( location[0], location[1], marker=(xy,0), s=size, facecolor=
               ['gold','yellow', 'orange', 'red','purple','indigo','violet'][i%7])

figure(1, figsize=(6,6))

little_pie([10,3,7],(1,1),600)
little_pie([10,27,4,8,4,5,6,17,33],(-1,1),800)

fracs = [10, 8, 7, 10]
explode=(0, 0, 0.1, 0)
pie(fracs, explode=explode, autopct='%1.1f%%')
show()

我还没有用过,但你可以试试:


特别是:

我找不到解决方案,所以我黑了自己的。我在matplotlib.patches模块中使用了ConnectionPatch对象。这允许您在同一图形中的不同轴之间绘制线。下面在左侧创建饼图,在右侧创建堆叠条:

import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np
import math

# style choice
plt.style.use('fivethirtyeight')

# make figure and assign axis objects
fig = plt.figure(figsize=(15,7.5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

# pie chart parameters
ratios = [.4, .56, .04]
labels = ['Approve', 'Disapprove', 'Undecided']
explode=[0.1,0,0]
# rotate so that first wedge is split by the x-axis
angle = -180*ratios[0]
ax1.pie(ratios, autopct='%1.1f%%', startangle=angle,
        labels=labels,explode=explode )

# bar chart parameters

xpos = 0
bottom = 0
ratios = [.33, .54, .07, .06]
width = .2
colors = ['y','m','#99ff99','#ffcc99']

for j in range(len(ratios)):
    height = ratios[j]
    ax2.bar(xpos, height, width, bottom=bottom, color=colors[j])
    ypos = bottom + ax2.patches[j].get_height()/2
    bottom += height
    ax2.text(xpos,ypos, "%d%%" %
        (ax2.patches[j].get_height()*100), ha='center')

plt.title('Gender of approvers')
plt.legend(('Women', 'Men', 'Gender Neutral', 'Alien'))
plt.axis('off')
plt.xlim(-2.5*width, 2.5*width)
然后,我添加两条线,分别将饼图的第一个楔子与堆叠条形图的顶部和底部连接起来:

# use ConnectionPatch to draw lines between the two plots
# get the wedge data for the first group
theta1, theta2 = ax1.patches[0].theta1, ax1.patches[0].theta2
center, r = ax1.patches[0].center, ax1.patches[0].r
bar_height = sum([item.get_height() for item in ax2.patches])
x = r*np.cos(math.pi/180*theta2)+center[0]
y = np.sin(math.pi/180*theta2)+center[1]
con = ConnectionPatch(xyA=(-width/2,bar_height), xyB=(x,y),
    coordsA="data", coordsB="data", axesA=ax2, axesB=ax1)
con.set_color([0,0,0])
con.set_linewidth(4)
ax2.add_artist(con)

x = r*np.cos(math.pi/180*theta1)+center[0]
y = np.sin(math.pi/180*theta1)+center[1]
con = ConnectionPatch(xyA=(-width/2,0), xyB=(x,y),
    coordsA="data", coordsB="data", axesA=ax2, axesB=ax1)
con.set_color([0,0,0])
ax2.add_artist(con)
con.set_linewidth(4)

plt.show()
图为:

matplotlib文档中现在有一个已发布的示例: