2

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()
1

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.