3

我有一个饼图(示例),其中包含以下内容fracs = [10, 20, 50, 30]。用 matplotlib 画这个没问题。如何将第一个楔子(10) 分解为64?理想情况下,我希望20, 分解为10, 3,的第二个楔子7。这将显示为特定楔形附近的条形图或饼图(这将使其成为类似于 Excel 中的饼图)。

4

3 回答 3

3

这是一种方法(可能不是最好的......)。我已经修改了一些在这里找到的代码,在 matplotlib 站点上创建一个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()

在此处输入图像描述

于 2012-05-18T15:31:26.167 回答
1

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

特别是:http: //pygal.org/en/stable/documentation/types/pie.html#multi-series-pie

于 2013-04-19T17:04:41.267 回答
1

我找不到解决方案,所以我自己破解了。我在 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()

这是情节: 饼图示例中的条形图

于 2018-08-14T19:21:47.780 回答