代码之家  ›  专栏  ›  技术社区  ›  Eric O. Lebigot

如何在Python中绘制顶部有第一个楔块的饼图?[matplotlib]库

  •  2
  • Eric O. Lebigot  · 技术社区  · 14 年前

    如何使用Matplotlib绘制饼图,并且第一个楔子从中午开始(即饼图顶部)?默认值为 pyplot.pie() 把第一个边缘放在三点钟,能定制这个就太好了。

    2 回复  |  直到 13 年前
        1
  •  2
  •   Nils Gudat    8 年前

    就因为这是我在Google搜索中发现的,我还要补充一点,matplotlib把它作为 pie 功能。

    现在,你可以打电话了 plt.pie(data, start_angle=90) 在中午开始第一个楔块。

    PyPlot documentation on this

        2
  •  5
  •   Joe Kington    14 年前

    这有点像黑客,但你可以这样做。。。

    import matplotlib.pyplot as plt
    from matplotlib.transforms import Affine2D
    import numpy as np
    
    x = [5, 20, 10, 10]
    labels=['cliffs', 'frogs', 'stumps', 'old men on tractors']
    
    plt.figure()
    plt.suptitle("Things I narrowly missed while learning to drive")
    wedges, labels = plt.pie(x, labels=labels)
    plt.axis('equal')
    
    starting_angle = 90
    rotation = Affine2D().rotate(np.radians(starting_angle))
    
    for wedge, label in zip(wedges, 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.show()
    

    Example Pie Plot