首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在matplotlib中使用楔形图绘制特定角度的饼图

在matplotlib中使用楔形图绘制特定角度的饼图
EN

Stack Overflow用户
提问于 2012-02-10 08:12:30
回答 1查看 7K关注 0票数 2

我正在用matplotlib绘制一个片段图,代码如下:

代码语言:javascript
复制
ax = axes([0.1, 0.1, 0.6, 0.6])
labels = 'Twice Daily', 'Daily', '3-4 times per week', 'Once per week','Occasionally'
fracs = [20,50,10,10,10]

explode=(0, 0, 0, 0,0.1)
patches, texts, autotexts = ax.pie(fracs, labels=labels, explode = explode,         
                             autopct='%1.1f%%', shadow =True)
proptease = fm.FontProperties()
proptease.set_size('xx-small')
setp(autotexts, fontproperties=proptease)
setp(texts, fontproperties=proptease)
rcParams['legend.fontsize'] = 7.0
savefig("pie1")

这将生成以下piechart。

但是,我想从顶部的第一个楔形开始绘制饼图,我能找到的唯一解决方案就是使用this code

然而,在使用它时,如下所示:

代码语言:javascript
复制
from pylab import *
from matplotlib import font_manager as fm
from matplotlib.transforms import Affine2D
from matplotlib.patches import Circle, Wedge, Polygon
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)

labels = 'Twice Daily', 'Daily', '3-4 times per week', 'Once per week','Occasionally'
fracs = [20,50,10,10,10]

 wedges, plt_labels = ax.pie(fracs, labels=labels)
 ax.axis('equal')

 starting_angle = 90
 rotation = Affine2D().rotate(np.radians(starting_angle))

for wedge, label in zip(wedges, plt_labels):
  label.set_position(rotation.transform(label.get_position()))
  if label._x > 0:
    label.set_horizontalalignment('left')
  else:
    label.set_horizontalalignment('right')

  wedge._path = wedge._path.transformed(rotation)

plt.savefig("pie2")

这将生成以下饼图

但是,这不会像前面的饼图那样在楔形上打印分数。我已经尝试了一些不同的东西,但我不能保留这些碎片。如何在中午启动第一个楔形,并在楔形上显示分数?

EN

回答 1

Stack Overflow用户

发布于 2012-02-10 09:23:37

通常,我不建议更改工具的源代码,但在外部修复这个问题是很麻烦的,内部也很容易。所以,如果你现在需要这个工具(Tm),我会这么做,有时候你确实需要。

在文件matplotlib/axes.py中,将pie函数的声明更改为

代码语言:javascript
复制
def pie(self, x, explode=None, labels=None, colors=None,
        autopct=None, pctdistance=0.6, shadow=False,
        labeldistance=1.1, start_angle=None):

即,只需将start_angle=None添加到参数的末尾。

然后添加用"# add“括起来的五行。

代码语言:javascript
复制
    for frac, label, expl in cbook.safezip(x,labels, explode):
        x, y = center
        theta2 = theta1 + frac
        thetam = 2*math.pi*0.5*(theta1+theta2)

        # addition begins here
        if start_angle is not None and i == 0:
            dtheta = (thetam - start_angle)/(2*math.pi)
            theta1 -= dtheta
            theta2 -= dtheta
            thetam = start_angle
        # addition ends here

        x += expl*math.cos(thetam)
        y += expl*math.sin(thetam)

如果start_angle为None,则什么也不会发生,但是如果start_angle有一个值,那么这就是第一个片(在本例中是20%)所在的位置。例如,

代码语言:javascript
复制
patches, texts, autotexts = ax.pie(fracs, labels=labels, explode = explode,         
                             autopct='%1.1f%%', shadow =True, start_angle=0.75*pi)

产生

请注意,一般来说,您应该避免这样做,我的意思是修补源代码,但在过去的一些时候,我已经到了最后期限,只是想要一些Now(tm),所以就这样吧。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9220933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档