【问题标题】:Plotting a pie-chart in matplotlib at a specific angle with the fracs on the wedges在 matplotlib 中以特定角度绘制饼图,楔形上的裂缝
【发布时间】:2012-02-10 00:12:30
【问题描述】:

我正在使用以下代码使用 matplotlib 绘制饼图:

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")

这将产生以下饼图。

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

但是,如下使用它,

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")

这将产生以下饼图

但是,这不会像之前的饼图中那样在楔形上打印分数。我尝试了一些不同的东西,但我无法保留碎片。如何在中午开始第一个楔形并在楔形上显示裂缝??

【问题讨论】:

    标签: python matplotlib pie-chart wedge


    【解决方案1】:

    通常我不建议更改工具的来源,但是在外部解决这个问题并在内部轻松解决这个问题很麻烦。所以如果你现在需要它来工作,我会这样做(tm),有时你会这样做..

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

    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 添加到参数的末尾即可。

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

        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%)的中心位置。例如,

    patches, texts, autotexts = ax.pie(fracs, labels=labels, explode = explode,         
                                 autopct='%1.1f%%', shadow =True, start_angle=0.75*pi)
    

    生产

    请注意,一般情况下,您应该避免这样做,我的意思是修补源代码,但过去有时我已经到了截止日期,只是现在想要一些东西(tm),所以你去吧..

    【讨论】:

      猜你喜欢
      • 2018-12-26
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多