Labels show up interactively on click in python matplotlib
<p>I am plotting the following numpy array (plotDataFirst), which has 40 x 160 dimensions (and contains double values).</p>

<p>I would like to be able to hover over a plot (one of the 40 that are drawn) and see the label of that particular plot. </p>

<p>I have an array (1x40) that contains all of the labels. Is there any way to do this? I am not sure how to add this type of interactive labels. </p>

<pre><code>plt.interactive(False)
plt.plot(plotDataFirst)
plt.show()
</code></pre>

<p>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:</p>

<pre><code>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()
</code></pre>

<p>So basically, for every mouse motion (<code>motion_notify_event</code>), 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.</p>

<p>Using a tooltip might be a nicer approach, but that seems to require backend-specific solutions (see e.g. <a href="http://stackoverflow.com/a/4620352/3581217">http://stackoverflow.com/a/4620352/3581217</a>) </p>

python numpy matplotlib