简体   繁体   English

防止情节显示在 jupyter notebook 中

[英]prevent plot from showing in jupyter notebook

How can I prevent a specific plot to be shown in Jupyter notebook?如何防止在 Jupyter 笔记本中显示特定图? 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: Jupyter 笔记本的最小工作示例是:

%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.如您所见,我有两种类型的图,a 和 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.我想要绘制和显示 a,我不想显示 b 图,我只想将它们保存在一个文件中。 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感谢您的时间

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.不会在内inline模式下绘制输出。 I can't work out if is really clearing the data though.我不知道是否真的在清除数据。

I was able to prevent my figures from displaying by turning interactive mode off using the function我能够通过使用该功能关闭交互模式来阻止我的数字显示

plt.ioff() plt.ioff()

To prevent any output from a jupyter notebook cell you may start the cell with为了防止来自 jupyter notebook 单元的任何输出,您可以使用以下命令启动单元

%%capture

This might be usefull in cases all other methods shown here fail.这在此处显示的所有其他方法失败的情况下可能很有用。

From IPython 6.0 on, there is another option to turn the inline output off (temporarily or persistently).从 IPython 6.0 开始,还有另一个选项可以关闭内联输出(暂时或永久)。 This has been introduced in this pull request .这已在此拉取请求中引入。

You would use the "agg" backend to not show any inline output.您将使用“agg”后端不显示任何内联输出。

%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这是它在行动中的样子

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.希望能帮助到你。

On Jupyter 6.0, I use the following snippet to selectively not display the matplot lib figures.在 Jupyter 6.0 上,我使用以下代码片段选择性地不显示 matplot lib 图形。

import matplotlib as mpl

...

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

# Your code

mpl.use(backend_) # Reset backend

Building off @importanceofbeingernest's answer, one may call some function in a loop, and at each iteration, want to render a plot.基于@importanceofbeingernest 的答案,人们可​​能会在循环中调用某个函数,并且在每次迭代时都想要渲染一个图。 However, between the each plot, you may want to render additional stuff.但是,在每个图之间,您可能想要渲染其他内容。

Concretely:具体来说:

  1. Iterate a list of IDs迭代 ID 列表
  2. Call a function so a plot is rendered for each "ID"调用一个函数,以便为每个“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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM