I want to build a pie chart, however my labels keep overlapping (I have one large and ten small wedges). After some research I came across the answer to a similar question, however it does not work for me, this is the output:
Is there a way to reliably annotate my pie chart such that the labels do not overlap or partly cross into the pie itself?
This is my code:
import matplotlib.pyplot as plt
import math
labels = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven"]
fig, ax = plt.subplots()
values = [574.51,
272.3333,
250.0,
221.0,
164.0,
135.0,
114.10000000000001,
112.31708,
100.0,
91.8,
2209.0827639999993]
l = ax.pie(values, startangle=-90)
for label, t in zip(labels, l[1]):
x, y = t.get_position()
angle = int(math.degrees(math.atan2(y, x)))
ha = "left"
va = "bottom"
if angle > 90:
angle -= 180
if angle < 0:
va = "top"
if -45 <= angle <= 0:
ha = "right"
va = "bottom"
plt.annotate(label, xy=(x,y), rotation=angle, ha=ha, va=va, size=8)