Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

graph - Matplotlib / python clickable points

I have a bunch of time series data with points every 5 seconds. So, I can create a line plot and even smooth the data to have a smoother plot. The question is, is there any way in matplotlib or anything in python that will allow me to click on a valid point to do something? So, for example, I would be able to click on (10, 75) if that datum exists in my original data and then I would be able to do something in Python.

Any thoughts? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To expand on what @tcaswell said, see the documentation here: http://matplotlib.org/users/event_handling.html

However, you might find a quick demo of pick events useful:

import matplotlib.pyplot as plt

def on_pick(event):
    artist = event.artist
    xmouse, ymouse = event.mouseevent.xdata, event.mouseevent.ydata
    x, y = artist.get_xdata(), artist.get_ydata()
    ind = event.ind
    print 'Artist picked:', event.artist
    print '{} vertices picked'.format(len(ind))
    print 'Pick between vertices {} and {}'.format(min(ind), max(ind)+1)
    print 'x, y of mouse: {:.2f},{:.2f}'.format(xmouse, ymouse)
    print 'Data point:', x[ind[0]], y[ind[0]]
    print

fig, ax = plt.subplots()

tolerance = 10 # points
ax.plot(range(10), 'ro-', picker=tolerance)

fig.canvas.callbacks.connect('pick_event', on_pick)

plt.show()

Exactly how you approach this will depend on what artist you're using (In other words, did you use ax.plot vs. ax.scatter vs. ax.imshow?).

Pick events will have different attributes depending on the artist selected. There will always be event.artist and event.mouseevent. Most artists that have individual elements (e.g. Line2Ds, Collections, etc) will have a list of the index of the items selected as event.ind.

If you'd like to draw a polygon and select points inside, see: http://matplotlib.org/examples/event_handling/lasso_demo.html#event-handling-example-code-lasso-demo-py


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...