Interactive plotting
When we did the previous example, no plots appeared until show()
was called. Furthermore, no new commands could be entered into the Python interpreter until all the figures were closed. As you will soon learn, once a figure is closed, the plot it contains is lost, which means that you would have to repeat all the commands again in order to show()
it again, perhaps with some modification or additional plot. Matplotlib ships with its interactive plotting mode off by default.
There are a couple of ways to turn the interactive plotting mode on. The main way is by calling the ion()
function (for Interactive ON). Interactive plotting mode can be turned on at any time and turned off with ioff()
. Once this mode is turned on, the next plotting command will automatically trigger an implicit show()
command. Furthermore, you can continue typing commands into the Python interpreter. You can modify the current figure, create new figures, and close existing ones at any time, all from the current Python session.