matplotlib 光标下的多个值 [英] matplotlib multiple values under cursor

查看:44
本文介绍了matplotlib 光标下的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与此处回答的问题非常相似

解决方案

您可能可以在此示例的基础上进行构建.它不显示图形中的信息(目前仅使用 print()语句),但是它演示了一种简单的方法来捕获 scatter 点上的点击并显示这些点的信息:

将 numpy 导入为 np导入matplotlib.pylab作为plpl.close('all')n = 10lat = np.random.random(n)lon = np.random.random(n)速度= np.random.random(n)rpm = np.random.random(n)temp = np.random.random(n)def on_click(事件):i = event.ind[0]打印('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]))图=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.43lon = 0.41,lat = 0.07:V = 0.95,RPM = 0.59,T = 0.98lon=0.86, lat=0.13: V=0.33, RPM=0.27, T=0.85

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?

解决方案

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

这篇关于matplotlib 光标下的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆