How to add hovering annotations in matplotlib How to add hovering annotations in matplotlib python python

How to add hovering annotations in matplotlib


It seems none of the other answers here actually answer the question. So here is a code that uses a scatter and shows an annotation upon hovering over the scatter points.

import matplotlib.pyplot as pltimport numpy as np; np.random.seed(1)x = np.random.rand(15)y = np.random.rand(15)names = np.array(list("ABCDEFGHIJKLMNO"))c = np.random.randint(1,5,size=15)norm = plt.Normalize(1,4)cmap = plt.cm.RdYlGnfig,ax = plt.subplots()sc = plt.scatter(x,y,c=c, s=100, cmap=cmap, norm=norm)annot = ax.annotate("", xy=(0,0), xytext=(20,20),textcoords="offset points",                    bbox=dict(boxstyle="round", fc="w"),                    arrowprops=dict(arrowstyle="->"))annot.set_visible(False)def update_annot(ind):    pos = sc.get_offsets()[ind["ind"][0]]    annot.xy = pos    text = "{}, {}".format(" ".join(list(map(str,ind["ind"]))),                            " ".join([names[n] for n in ind["ind"]]))    annot.set_text(text)    annot.get_bbox_patch().set_facecolor(cmap(norm(c[ind["ind"][0]])))    annot.get_bbox_patch().set_alpha(0.4)def hover(event):    vis = annot.get_visible()    if event.inaxes == ax:        cont, ind = sc.contains(event)        if cont:            update_annot(ind)            annot.set_visible(True)            fig.canvas.draw_idle()        else:            if vis:                annot.set_visible(False)                fig.canvas.draw_idle()fig.canvas.mpl_connect("motion_notify_event", hover)plt.show()

enter image description here

Because people also want to use this solution for a line plot instead of a scatter, the following would be the same solution for plot (which works slightly differently).