How can a pie chart be drawn with Matplotlib with a first wedge that starts at noon (i.e. on the top of the pie)? 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.
views:
67answers:
1
+4
A:
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()
Joe Kington
2010-09-28 22:39:57
+1! This is sufficiently long for such a simple need that I wish that `pie()` had an additional `start_angle` parameter.
EOL
2010-09-29 09:07:52
@EOL - I agree, it seems awfully complicated for what should be a fairly common and simple need... Maybe an enhancement request for a `start_angle` parameter is a good idea? I can't imagine that you're the only person who has ever needed `pie()` to start at a different angle...
Joe Kington
2010-09-29 15:42:45