Issue
I am using plt.connect to save into a list of coordinates the points where my cursor passes, and the I would like to plot these points, but after I disconnect the plot I can not do anything and the plot closes by itself. I can not find any example where, after connecting the plot, you can do whatever with the plot. This is the code I am trying to fix:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backend_bases import MouseButton
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s) #just plot something
x=[]
y=[]
def on_move(event):
global x,y
if event.inaxes:
x.append(event.xdata)
y.append(event.ydata)
print('data coords %f %f' % (event.xdata, event.ydata))
plt.waitforbuttonpress()
binding_id = plt.connect('motion_notify_event', on_move)
plt.pause(5) #5 seconds to collect data
plt.disconnect(binding_id) #here it closes by itself
ax.plot(x,y)
Solution
only put a pause again in the last line. This will keep the graph and you will can see the lines.
...
ax.plot(x,y)
plt.pause(5)
Answered By - Gustavo Alves
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.