1

This question is very similar to those answered here,

matplotlib values under cursor

In a matplotlib figure window (with imshow), how can I remove, hide, or redefine the displayed position of the mouse?

Interactive pixel information of an image in Python?

except that instead of pixel data (x,y,z) I have various measurements associated with (x,y) coordinates that I'd like to portray on a line plot. Specifically, the (x,y) are spatial positions (lat, lon) and at each (lat,lon) point there is a collection of data (speed, RPM, temp, etc.). I just sketched up something quickly to illustrate, a scatter plot with connecting lines, and then when you hover over a data point it displays all of the "z" values associated with that data point.

Is there an easy way to do something like this?

enter image description here

Community
  • 1
  • 1
astronomerdave
  • 512
  • 1
  • 5
  • 18
  • Possible duplicate of [Interactive pixel information of an image in Python?](http://stackoverflow.com/questions/27704490/interactive-pixel-information-of-an-image-in-python) – tmdavison Sep 07 '16 at 08:36

1 Answers1

2

You could probably build on something like this example. It doesn't display the information inside the figure (for now only using a print() statement), but it demonstrates a simple method of capturing clicks on scatter points and showing information for those points:

import numpy as np
import matplotlib.pylab as pl

pl.close('all')

n = 10
lat = np.random.random(n)
lon = np.random.random(n)

speed = np.random.random(n)
rpm   = np.random.random(n)
temp  = np.random.random(n)

def on_click(event):
    i = event.ind[0]
    print('lon={0:.2f}, lat={1:.2f}: V={2:.2f}, RPM={3:.2f}, T={4:.2f}'\
        .format(lon[i], lat[i], speed[i], rpm[i], temp[i])) 

fig=pl.figure()
pl.plot(lon, lat, '-')
pl.scatter(lon, lat, picker=True)
fig.canvas.mpl_connect('pick_event', on_click)

Clicking around a bit gives me:

lon=0.63, lat=0.58: V=0.51, RPM=0.00, T=0.43
lon=0.41, lat=0.07: V=0.95, RPM=0.59, T=0.98
lon=0.86, lat=0.13: V=0.33, RPM=0.27, T=0.85
Bart
  • 9,825
  • 5
  • 47
  • 73