I'm not sure exactly how you want to show the label (tooltip, legend, title, label, ...), but something like this might be a first step:
import numpy as np
import matplotlib.pylab as pl
pl.close('all')
def line_hover(event):
ax = pl.gca()
for line in ax.get_lines():
if line.contains(event)[0]:
print(line.get_label())
labels = ['line 1','line 2','line 3']
fig = pl.figure()
for i in range(len(labels)):
pl.plot(np.arange(10), np.random.random(10), label=labels[i])
pl.legend(frameon=False)
fig.canvas.mpl_connect('motion_notify_event', line_hover)
pl.show()
So basically, for every mouse motion (motion_notify_event
), check if the cursor is over one of the lines, and if so, (as a quick hack / solution for now), print the label of that line to the command line.
Using a tooltip might be a nicer approach, but that seems to require backend-specific solutions (see e.g. https://stackoverflow.com/a/4620352/3581217)