如何使用Matplotlib绘制一个饼图,使第一块扇形从正上方开始(即从12点钟方向开始)?pyplot.pie()
的默认设置是第一块扇形从3点钟方向开始,如果能够自定义设置就更好了。
如何使用Matplotlib绘制一个饼图,使第一块扇形从正上方开始(即从12点钟方向开始)?pyplot.pie()
的默认设置是第一块扇形从3点钟方向开始,如果能够自定义设置就更好了。
这有点像是一种技巧,但你可以这样做...
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()
pie()
函数有一个额外的 start_angle
参数。 - Eric O. Lebigotpie()
从不同角度开始的人... - Joe Kington只是因为这个在谷歌搜索中出现了,我要补充说,与此同时,matplotlib已经包含了这个作为pie
函数的额外参数。
现在,可以调用plt.pie(data, startangle=90)
来使第一个楔形从正午开始。