简体   繁体   English

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

[英]How to plot a pie chart with the first wedge on top, in Python? [matplotlib]

How can a pie chart be drawn with Matplotlib with a first wedge that starts at noon (ie on the top of the pie)? 如何用Matplotlib绘制一个饼图,该饼图具有从中午开始的第一个楔形(即在饼的顶部)? The default is for pyplot.pie() to place the first edge at three o'clock, and it would be great to be able to customize this. pyplot.pie()的默认设置是将第一条边放置在三点钟位置,并且能够自定义它会很棒。

It's a bit of a hack, but you can do something like this... 这有点骇人听闻,但您可以执行以下操作...

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

饼图示例

Just because this came up in a Google search for me, I'll add that in the meantime, matplotlib has included just this as an additional argument to the pie function. 只是因为这是在Google搜索中出现的,所以我要补充一点,与此同时,matplotlib将此包含为pie函数的附加参数。

Now, one can call plt.pie(data, start_angle=90) to have the first wedge start at noon. 现在,可以调用plt.pie(data, start_angle=90)使第一个楔形块在中午开始。

PyPlot documentation on this 有关此的PyPlot文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM