matplotlib游标下的多个值

时间:2016-09-07 03:23:28

标签: python matplotlib

这个问题与这里回答的问题非常相似,

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?

除了代替像素数据(x,y,z),我有各种与(x,y)坐标相关的测量值,我想在线图上描绘。具体地,(x,y)是空间位置(lat,lon),并且在每个(lat,lon)点处存在数据集合(速度,RPM,温度等)。我只是快速勾勒出一些东西来说明,一个带有连接线的散点图,然后当你将鼠标悬停在一个数据点上时,它会显示与该数据点相关的所有“z”值。

有没有一种简单的方法可以做这样的事情?

enter image description here

1 个答案:

答案 0 :(得分:1)

你可以建立像这个例子的东西。它没有在图中显示信息(现在只使用print()语句),但它演示了一种简单的方法来捕获scatter点上的点击并显示这些点的信息:< / p>

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)

点击一下就可以了:

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