如何在Python中绘制第一个扇区在顶部的饼图?[matplotlib]
如何用Matplotlib画一个饼图,让第一个扇形从正上方开始(也就是从12点钟的位置)?默认情况下,pyplot.pie()
会把第一个扇形放在三点钟的位置,如果能自定义这个就太好了。
2 个回答
6
这有点像小技巧,但你可以这样做...
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()
6
因为我在谷歌搜索时遇到了这个问题,所以我想补充一下,matplotlib现在在pie
函数中增加了这个功能,可以作为一个额外的参数。
现在,你可以用plt.pie(data, startangle=90)
来让第一个扇形从正上方开始。