Reputation: 121
, 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()
In the plot there will be 19 dots from 0,0
to 1.9,1.9
; so...
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???
Upvotes: 0
Views: 6310
Reputation: 339170
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()
Upvotes: 1