Is there a way to detach matplotlib plots so that the computation can continue?


Question

After these instructions in the Python interpreter one gets a window with a plot:

from matplotlib.pyplot import *
plot([1,2,3])
show()
# other code

Unfortunately, I don't know how to continue to interactively explore the figure created by show() while the program does further calculations.

Is it possible at all? Sometimes calculations are long and it would help if they would proceed during examination of intermediate results.

1
237
12/27/2015 6:50:09 AM

Accepted Answer

Use matplotlib's calls that won't block:

Using draw():

from matplotlib.pyplot import plot, draw, show
plot([1,2,3])
draw()
print 'continue computation'

# at the end call show to ensure window won't close.
show()

Using interactive mode:

from matplotlib.pyplot import plot, ion, show
ion() # enables interactive mode
plot([1,2,3]) # result shows immediatelly (implicit draw())

print 'continue computation'

# at the end call show to ensure window won't close.
show()
199
10/26/2015 11:09:47 PM

Use the keyword 'block' to override the blocking behavior, e.g.

from matplotlib.pyplot import show, plot

plot(1)  
show(block=False)

# your code

to continue your code.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon