I want to add an event to a pie chart what, upon hovering, shows an annotation with the value and label of what wedge. After a while of trying, I got a working example for line plot, but for pie charts I can't understand how to obtain the data of each wedge. My code so far:
from matplotlib.figure import Figure
figure = Figure()
drawFigure = figure.subplots()
annotate = drawFigure.annotate("", xy=(0,0),xytext=(-20, 20), textcoords="offset points",
bbox=dict(boxstyle="round", fc="black", ec="b", lw=2),
arrowprops=dict(arrowstyle="->"))
annotate.set_visible(False)
days, amounts = zip(*my_dict.items())
wedges, _ = drawFigure.pie(amounts, labels=None)
drawFigure.axis('equal')
figure.canvas.mpl_connect("motion_notify_event", Hover)
def UpdateAnnotation(ind):
data = ind.get_patch_transform() # I'm not sure this does anything useful
# what now? can't seem to get anything working
def Hover(event):
vis = annotate.get_visible()
if event.inaxes == drawFigure:
for i, w in enumerate(wedges):
if w.contains_point([event.x, event.y]):
print("hovering")
figure.canvas.draw_idle()
Ideally, I'd like that when I hover over a wedge, an annotation pops up with the key : value
pair corresponding to that wedge. Also, exploding that wedge would be nice.