1

I am plotting the following numpy array (plotDataFirst), which has 40 x 160 dimensions (and contains double values).

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.

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.

plt.interactive(False)
plt.plot(plotDataFirst)
plt.show()
dorien
  • 5,265
  • 10
  • 57
  • 116
  • You may want to look at http://matplotlib.org/examples/event_handling/legend_picking.html and http://matplotlib.org/users/event_handling.html – user2699 Oct 19 '16 at 16:58

1 Answers1

3

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)

Community
  • 1
  • 1
Bart
  • 9,825
  • 5
  • 47
  • 73