prevent plot from showing in jupyter notebook

后端 未结 6 2167
梦毁少年i
梦毁少年i 2020-12-04 23:19

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

相关标签:
6条回答
  • 2020-12-04 23:53

    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
    
    0 讨论(0)
  • 2020-12-04 23:54

    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

    0 讨论(0)
  • 2020-12-04 23:55

    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.

    0 讨论(0)
  • 2020-12-05 00:08

    Perhaps just clear the axis, for example:

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

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

    0 讨论(0)
  • 2020-12-05 00:08

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

    plt.ioff()

    0 讨论(0)
  • 2020-12-05 00:08

    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.

    0 讨论(0)
提交回复
热议问题