I'm looking for a a method to generate interactive figures that works in Windows and Linux, commandline or Spyder. For example, the following script:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1, tight_layout=True)
#plt.ion() # doesn't seem to do much
ax.plot([0,1,2],[0,3,2])
fig.show()
#plt.show(block=False) # similar behavior as fig.show()
#plt.pause(1)
input('Press enter to quit.')
Behavior for different environments:
Linux command line: plot window shows up and is responsive while the script waits for user input. The window stays even if the program continues running (not in this short example), although the zoom buttons don't work anymore. This is desired behavior.
Windows command line: an empty nonresponsive plot window shows up, which disappears when the program ends. Adding
plt.pause()
results in an interactive plot, but it is only responsive for the specified number of seconds.Linux/Windows Spyder with iPython, configured for automatic plots: figures show up and are responsive, but only after the script finishes.
Linux/Windows Spyder, configured for inline plots: plots show up after the script finishes, but with warnings due to the
tight_layout=True
parameter: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. and matplotlib is currently using a non-GUI backend. (Note that I needtight_layout
because otherwise often axis labels are clipped or figures with multiple subplots have bad margins.)Linux/Windows Spyder, inline plots: with
plt.plot(...); plt.show()
(rather than thefig.plot(...); fig.show()
object-oriented way), the inline plots show up during program execution.
How can I write code that generates interactive plots during program execution (possibly while waiting for a keypress) that will run correctly from the Windows command line, Linux command line, and Spyder?
Edit: plt.show()
instead of fig.show()
will result in a plot being shown correctly, but outside IPython, it blocks execution of the script until I click the close button of the figure window. This is rather cumbersome when there are multiple figures or when the calculation is not yet finished. And plt.show(block=False)
has the same behavior as fig.show()
.
I'm using an noncustomized Anaconda 5.1 environment with Python 3.6.4, matplotlib 2.1.2, spyder 3.2.6. In Spyder: Tools > Preferences > IPython > Backend, set to 'inline' or 'automatic' (and restarting kernel after changing this setting.)