问题描述
,大家好,我使用的是python> matplotlib,我想通过使用光标从图中获取数据.
, Hi guys, I'm using python>matplotlib and I want to get the data from the plot by using the cursor.
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()
在图中,从0,0
到1.9,1.9
会有19个点;所以...
In the plot there will be 19 dots from 0,0
to 1.9,1.9
; so...
当我先单击0,0
然后单击0.3,0.3
时,我想获取值:
When I click on 0,0
first and then on 0.3,0.3
, I want to get the values:
(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???
推荐答案
有一个在matplotlib页面上的选择器示例.您可以对其进行调整,以在单击第n个点时显示前n个点对.
There is a Picker example on the matplotlib page. You can adapt it to show the first n point pairs when the nth point is clicked.
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()
这篇关于Python Matplotlib从绘图中使用光标获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!