prevent plot from showing in jupyter notebook
Asked Answered
S

7

121

How can I prevent a specific plot to be shown in Jupyter notebook? I have several plots in a notebook but I want a subset of them to be saved to a file and not shown on the notebook as this slows considerably.

A minimal working example for a Jupyter notebook is:

%matplotlib inline 
from numpy.random import randn
from matplotlib.pyplot import plot, figure
a=randn(3)
b=randn(3)
for i in range(10):
    fig=figure()
    plot(b)
    fname='s%03d.png'%i
    fig.savefig(fname)
    if(i%5==0):
        figure()
        plot(a)

As you can see I have two types of plots, a and b. I want a's to be plotted and shown and I don't want the b plots to be shown, I just want them them to be saved in a file. Hopefully this will speed things a bit and won't pollute my notebook with figures I don't need to see.

Thank you for your time

Saxecoburggotha answered 10/9, 2013 at 11:34 Comment(3)
possible duplicate of Plot an IPython Notebook figure inline with fig.show()?Fantast
honestly, I didn't understand the answers on that questionSaxecoburggotha
For more info and similar issue, look at this: #15713779Publea
A
155

Perhaps just clear the axis, for example:

fig = plt.figure()
plt.plot(range(10))
fig.savefig("save_file_name.pdf")
plt.close()

This will not plot the output in inline mode. I can't work out if it is really clearing the data though.

Archean answered 10/9, 2013 at 11:47 Comment(5)
It does not plot the output but returns <matplotlib.figure.Figure at 0x31c0d50> how can I prevent this from happening?Saxecoburggotha
I have updated the answer, using plt.close() doesn't output this. I think it is some kind of reference to the figure left over but I'm not sure.Archean
This is subtle, but changing plt.close() to plt.close(); should do the trick. This worked for me on IPython 2 and matplotlib 1.3.1Heriberto
In my case, using notebook mode, this method still creates some empty space in the output cell, which clutters my notebook when doing multiple plots at once. I wrapped my plotting routine in a pair of plt.ioff() and plt.ion() and now there is no more clutter.Coaction
Documentation link: matplotlib.pyplot.close — Matplotlib 3.2.1 documentationReturnable
F
58

I was able to prevent my figures from displaying by turning interactive mode off using the function

plt.ioff()

Favor answered 11/3, 2016 at 20:51 Comment(2)
Very helpful if you're using mpld3 (or something similar) for making interactive online figures and %matplotlib notebook to preview static figures.Snowdrop
@Brian Why should this be the new accepted answer? How is this method better than the previous? Is it more general?Saxecoburggotha
C
47

To prevent any output from a jupyter notebook cell you may start the cell with

%%capture

This might be usefull in cases all other methods shown here fail.

Crosse answered 10/8, 2017 at 14:52 Comment(3)
This is the best answer by far.Thresathresh
How would this be done in a jupyter-lab notebook running R? I get a Error in parse(text = x, srcfile = src): <text>:1:1: unexpected SPECIAL 1: %%Daisy
I guess if there are errors, you have to switch it on and offAntiquary
C
20

From IPython 6.0 on, there is another option to turn the inline output off (temporarily or persistently). This has been introduced in this pull request.

You would use the "agg" backend to not show any inline output.

%matplotlib agg

It seems though that if you had activated the inline backend first, this needs to be called twice to take effect.

%matplotlib agg
%matplotlib agg

Here is how it would look in action

Crosse answered 28/3, 2018 at 23:23 Comment(1)
Do you know why agg needs to be called twice if inline was already called?Tannen
P
10

On Jupyter 6.0, I use the following snippet to selectively not display the matplot lib figures.

import matplotlib as mpl

...

backend_ =  mpl.get_backend() 
mpl.use("Agg")  # Prevent showing stuff

# Your code

mpl.use(backend_) # Reset backend
Prudi answered 25/9, 2019 at 7:47 Comment(0)
N
9

I'm a beginner though,off the inline mode when you don't want to see the output in your notebook by:

%matplotlib auto

or:

%matplotlib

to use it back:

%matplotlib inline

more better solution would be to use:

plt.ioff()

which says inline mode off.

hope it helps.

Nork answered 11/12, 2017 at 11:25 Comment(0)
O
0

Building off @importanceofbeingernest's answer, one may call some function in a loop, and at each iteration, want to render a plot. However, between the each plot, you may want to render additional stuff.

Concretely:

  1. Iterate a list of IDs
  2. Call a function so a plot is rendered for each "ID"
  3. Between each plot, render some markdown

# <cell begins>
def render(id):
   fig, axes = plt.subplots(2, 1)
   plt.suptitle(f'Metrics for {id}')

   df.ColA.plot.bar(ax=axes[0])
   df.ColB.plot.bar(ax=axes[1])

   return fig

# <cell ends>

# -------------------------------------

# <cell begins>
%matplotlib agg

for id in df.ID.value_counts().index:
   fig = render(id)

   display(fig)
   display(Markdown('---'))

# <cell ends>
Objectivity answered 13/1, 2021 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.