【问题标题】:prevent plot from showing in jupyter notebook防止情节在 jupyter notebook 中显示
【发布时间】:2013-09-14 02:26:21
【问题描述】:

如何防止在 Jupyter 笔记本中显示特定的绘图?我在笔记本中有几个绘图,但我希望将其中的一部分保存到一个文件中,并且不显示在笔记本上,因为这会大大减慢速度。

Jupyter notebook 的最小工作示例是:

%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)

如您所见,我有两种类型的图,a 和 b。我想要绘制和显示 a,我不希望显示 b 图,我只想将它们保存在文件中。希望这会加快速度,不会让我不需要看到的数字污染我的笔记本。

感谢您的宝贵时间

【问题讨论】:

标签: python matplotlib jupyter-notebook ipython-notebook figures


【解决方案1】:

以@importanceofbeingernest 的回答为基础,人们可能会在循环中调用某个函数,并且在每次迭代时,都想渲染一个绘图。但是,在每个情节之间,您可能需要渲染其他内容。

具体来说:

  1. 迭代一个 ID 列表
  2. 调用一个函数,以便为每个“ID”渲染一个绘图
  3. 在每个绘图之间,渲染一些降价

# <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>

【讨论】:

    【解决方案2】:

    在 Jupyter 6.0 上,我使用以下 sn-p 选择性地不显示 matplot lib 图形。

    import matplotlib as mpl
    
    ...
    
    backend_ =  mpl.get_backend() 
    mpl.use("Agg")  # Prevent showing stuff
    
    # Your code
    
    mpl.use(backend_) # Reset backend
    

    【讨论】:

      【解决方案3】:

      从 IPython 6.0 开始,有另一个选项可以关闭内联输出(暂时或永久)。这已经被介绍了in this pull request

      您将使用“agg”后端不显示任何内联输出。

      %matplotlib agg
      

      如果您先激活了内联后端,似乎需要调用两次才能生效。

      %matplotlib agg
      %matplotlib agg
      

      下面是实际效果

      【讨论】:

      • 如果已经调用了 inline,你知道为什么需要调用 agg 两次吗?
      【解决方案4】:

      虽然我是初学者,但当您不想在笔记本中看到输出时,请关闭内联模式:

      %matplotlib auto
      

      或:

      %matplotlib
      

      使用它:

      %matplotlib inline
      

      更好的解决方案是使用:

      plt.ioff()
      

      表示内联模式关闭。

      希望对你有帮助。

      【讨论】:

        【解决方案5】:

        为了防止来自 jupyter notebook 单元的任何输出,您可以在单元开始时使用

        %%capture
        

        这在此处显示的所有其他方法都失败的情况下可能很有用。

        【讨论】:

          【解决方案6】:

          我可以通过使用该功能关闭交互模式来阻止我的数字显示

          plt.ioff()

          【讨论】:

          • 如果您使用 mpld3(或类似的东西)制作交互式在线图形并使用 %matplotlib notebook 预览静态图形,这将非常有用。
          • @Brian 为什么这应该是新接受的答案?这种方法比以前的方法好在哪里?是不是更笼统?
          【解决方案7】:

          也许只是清轴,例如:

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

          不会在inline 模式下绘制输出。不过我不知道是否真的在清除数据。

          【讨论】:

          • 它不绘制输出但返回 我怎样才能防止这种情况发生?
          • 我已经更新了答案,使用plt.close() 不会输出这个。我认为这是对剩余数字的某种参考,但我不确定。
          • 这很微妙,但是将 plt.close() 更改为 plt.close(); 应该可以解决问题。这在 IPython 2 和 matplotlib 1.3.1 上对我有用
          • 在我的情况下,使用notebook 模式,此方法仍会在输出单元格中创建一些空白空间,这会在一次进行多个绘图时使我的笔记本变得混乱。我将我的绘图程序包裹在一对plt.ioff()plt.ion() 中,现在不再混乱了。
          猜你喜欢
          • 2018-11-27
          • 2022-06-22
          • 1970-01-01
          • 2021-04-20
          • 2016-06-25
          • 1970-01-01
          • 2019-01-28
          • 1970-01-01
          相关资源
          最近更新 更多