0

In the Spyder IDE, I want to keep the inline console plotting (I don't want separate windows to spawn for each plot), but I want to programmatically disable plotting, i.e. in different cells.

In my workflow I need to plot a few simple graphs, and then generate figures and save them as video frames (many thousands). My frames are created by loading a jpg image, and then overlaying some annotation i.e.;

for jpg_path in path_list:
    img = mpl.image.imread(jpg_path)
    ax.imshow(img)
    ax.text(etc...)
    fig.savefig(etc...)

I want to keep the inline backend; %matplotlib inline.

But turn off plotting with something like plt.ioff().

But plt.ioff()only works with i.e. %matplotlib qt backend, not inline!

I've had several cases where I forget to change to %matplotlib qt (because it's not a python command and I have to enter it into the console seperately) and then plt.ioff() - resulting in 10000 images being posted in the console, freezing my machine.

Marcus Jones
  • 960
  • 3
  • 11
  • 27
  • 1
    Did you already look [here](https://stackoverflow.com/questions/49545003/how-to-suppress-matplotlib-inline-for-a-single-cell-in-jupyter-notebooks-lab), or [here](https://stackoverflow.com/questions/18717877/prevent-plot-from-showing-in-jupyter-notebook), or [here](https://stackoverflow.com/questions/30878666/matplotlib-python-inline-on-off)? – Sheldore Aug 31 '18 at 12:57
  • 1
    I'm a big fan of [using `%%capture`](https://stackoverflow.com/a/45617081/4124317). – ImportanceOfBeingErnest Aug 31 '18 at 13:15

1 Answers1

1

Ok I think I found an answer, thanks to this answer;

https://stackoverflow.com/a/46360516/789215

The key was the python command for the line magics get_ipython().run_line_magic('matplotlib', 'inline'). I created a context manager to wrap my video frame for-loop;

from IPython import get_ipython

class NoPlots:
    def __enter__(self):
        get_ipython().run_line_magic('matplotlib', 'qt')
        plt.ioff()
    def __exit__(self, type, value, traceback):
        get_ipython().run_line_magic('matplotlib', 'inline')
        plt.ion()

Or is there a better approach?

Marcus Jones
  • 960
  • 3
  • 11
  • 27