Python 防止情节显示在 jupyter notebook 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18717877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
prevent plot from showing in jupyter notebook
提问by gota
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.
如何防止在 Jupyter 笔记本中显示特定图?我在笔记本中有几个图,但我希望将其中的一个子集保存到一个文件中,而不是在笔记本上显示,因为这会大大减慢速度。
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. 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.
如您所见,我有两种类型的图,a 和 b。我想要绘制和显示 a,我不想显示 b 图,我只想将它们保存在一个文件中。希望这会加快速度,并且不会用我不需要看到的数字污染我的笔记本。
Thank you for your time
感谢您的时间
采纳答案by Greg
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.
不会在inline
模式下绘制输出。我不知道是否真的在清除数据。
回答by Dr.PP
I was able to prevent my figures from displaying by turning interactive mode off using the function
我能够通过使用该功能关闭交互模式来阻止我的数字显示
plt.ioff()
plt.ioff()
回答by ImportanceOfBeingErnest
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.
这在此处显示的所有其他方法失败的情况下可能很有用。
回答by Deepak Joshi
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.
希望能帮助到你。
回答by ImportanceOfBeingErnest
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.
从 IPython 6.0 开始,还有另一个选项可以关闭内联输出(暂时或永久)。这已在此拉取请求中引入。
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
这是它在行动中的样子
回答by Asanga
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