简体   繁体   English

Python Matplotlib从图中获取光标数据

[英]Python matplotlib get data with cursor from plot

, Hi guys, I'm using python>matplotlib and I want to get the data from the plot by using the cursor. ,大家好,我使用的是python> matplotlib,我想通过使用光标从图中获取数据。

import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0., 2., 0.1)
plt.plot(t,t,'g^')
ax = plt.gca()
line = ax.lines[0]
xd = line.get_xdata()
yd = line.get_ydata()
valx = np.where(xd==xd[0])
plt.show()

In the plot there will be 19 dots from 0,0 to 1.9,1.9 ; 在图中,从0,01.9,1.9会有19个点; so... 所以...

When I click on 0,0 first and then on 0.3,0.3 , I want to get the values: 当我先单击0,0然后再单击0.3,0.3 ,我想获取值:

(0,0);
(0.1,0.1);
(0.2,0.2);
(0.3,0.3)

Is there a way to do this? 有没有办法做到这一点?

But also there's a problem that the cursor has to be over the point, is there a way to position the cursor on the graphic and not other point??? 但是还有一个问题是光标必须在该点上,有没有办法将光标定位在图形而不是其他点上???

There is a Picker example on the matplotlib page. matplotlib页面上有一个Picker示例 You can adapt it to show the first n point pairs when the nth point is clicked. 您可以对其进行调整,以在单击第n个点时显示前n个点对。

import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0., 2., 0.1)
line, = plt.plot(t,t,'g^', picker=6)

def click(event):
    artist = event.artist
    ind = event.ind[0]
    xd = artist.get_xdata()[:ind]
    yd = artist.get_ydata()[:ind]
    print( zip(xd, yd) )

cid = plt.gcf().canvas.mpl_connect("pick_event", click)

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM