如何使用 Matplotlib 绘制饼图,其第一个楔形从中午开始(即在饼图的顶部)?默认设置是pyplot.pie()
把第一条边放在三点钟位置,如果能够自定义它就太好了。
问问题
3029 次
2 回答
5
这有点像黑客,但你可以做这样的事情......
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()
于 2010-09-28T22:39:57.217 回答
4
仅仅因为这出现在谷歌搜索中,我将同时补充一点,matplotlib 已将其作为pie
函数的附加参数包含在内。
现在,可以打电话plt.pie(data, start_angle=90)
让第一个楔子在中午开始。
于 2016-06-06T13:21:05.617 回答