Issue
I am using python's matplotlib
to draw figures.
I want to draw a figure with a timeout, say 3 seconds, and the window will close to move on the code.
I have known that pyplot.show()
will create a blocking window with unlimited timeout; pyplot.show(block=False)
or pyplot.draw()
will make the window non-blocking. But what I want is let the code block for some seconds.
I have come up an idea that I might use an event handler or something, but still not really clear how to solve this. Is there any simple and elegant solution?
Suppose my code is like the following:
Draw.py:
import matplotlib.pyplot as plt
#Draw something
plt.show() #Block or not?
Solution
Here's a simple example where I have created a timer to set your timeout and performed closing of window plot.close()
in the callback function of the timer. Start the timer before plot.show()
and after three seconds timer invokes close_event()
and then continues with the rest of the code.
import matplotlib.pyplot as plt
def close_event():
plt.close() #timer calls this function after 3 seconds and closes the window
fig = plt.figure()
timer = fig.canvas.new_timer(interval = 3000) #creating a timer object and setting an interval of 3000 milliseconds
timer.add_callback(close_event)
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
timer.start()
plt.show()
print("I am doing something else")
Hope that was helpful.
Answered By - Deepa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.