I'd like to do a task while the matplotlib figure is open, I'm looking for something like this:
while "figure is open instruction" True:
i = 0
p = 0
av_p = 0
for i in range(5):
if abs(position[0] - 2) <=10**-2
p = mass * np.sqrt(velocity[0][0]**2 + velocity[0][1]**2)
av_p = (p+av_p)/i+1
print(av_p)
So I'm trying to get a set of data during a certain time, and once the time is over the getting process of data turns back on.
If what you are trying to do is to update a plot (real time), may I suggest using pyqtgraph. Particularly, have a look in the examples, one of then show how to easily update the curve of a plot.
In any case, to check if a window is open, you can try something like this: How to detect that an axis belong to a window that has been closed in matplotlib
# defining on_close event
def on_close(event):
event.canvas.figure.axes[0].has_been_closed = True
# ploting
fig = plt.figure()
a = fig.canvas.mpl_connect('close_event', on_close)
ax = fig.add_subplot(111)
ax.has_been_closed = False
ax.plot([1, 2, 3], [2, 4, 6])
plt.show()
if ax.has_been_closed:
print('closed')
Then you can use ax.has_been_closed
to check if window is closed. Note that you have to have at least one axis defined.