Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one set keyword "block" in plt.show() equal to True by default?

Tags:

matplotlib

For some reason, I need to set the keyword "block" equal to True explicitly, so that plots are shown when I run a script from the bash shell. (I don't need that when I run it from the ipython shell). How can I set that argument to True by default as nearly everyone seems to have it?

Sample:

import matplotlib.pyplot as plt

plt.plot([1,2,3], [1,2,3])

plt.show(block=True) 

I would like plots to show up even if that argument isn't set to True explicitly, that is:

import matplotlib.pyplot as plt

plt.plot([1,2,3], [1,2,3])

plt.show() 

My matplotlibrc contains:

backend : MacOSX

interactive : True

toolbar : toolbar2
timezone : UTC

like image 377
drake Avatar asked Oct 27 '25 12:10

drake


1 Answers

The 'interactive' mode of mpl determines the behavior of plt.show. If in 'interactive' mode, it assumes that there is something else managing the GUI event loop. When running a script with

python -i script.py

will drop you into an interactive shell. When siting in the REPL, there is integration between the python REPL loop and the GUI event loop which allows the GUI loop to run in the background, which makes the figure 'interactive'. If 'interactive' mode was not on in this case, you would not get the prompt until you have closed the figure.

'interactive' mode can be enabled either by calling plt.ion() or by setting the 'interactive' key in matplotlibrc.

I strongly suggest that you leave the rcparam value to be False.

like image 83
tacaswell Avatar answered Oct 30 '25 06:10

tacaswell