Interactive Matplotlib Plots During Computation
When creating visualizations with Matplotlib, it's often desirable to continue exploring the results while computations are ongoing. However, the default behavior is to block computation until the show() function is called.
Detaching Plots
To detach plots from the main computation thread, there are two approaches:
Using draw():
This method allows for selective redrawing of the plot. Instead of calling show(), use draw() after plotting data. The computation will resume while the plot remains interactive. However, calling draw() multiple times may cause the plot to flicker.
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()
Enabling Interactive Mode:
This approach uses Matplotlib's interactive mode. Calling ion() enables the interactive mode, which automatically redraws the plot after each plotting command. The computation will continue while the plot can be interactively zoomed, panned, and investigated.
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()
By employing either of these approaches, it's possible to detach Matplotlib plots and allow computations to proceed in the background while interactively exploring the intermediate results.
The above is the detailed content of How to Make Matplotlib Plots Interactive During Computation?. For more information, please follow other related articles on the PHP Chinese website!