'%matplotlib inline'在以下代码中导致错误 [英] '%matplotlib inline' causes error in following code

查看:2069
本文介绍了'%matplotlib inline'在以下代码中导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我注释掉'%matplotlib inline',则代码运行良好 但是,如果我不加注释'%matplotlib inline',则'fig,axes = plt.subplots(nrows = x_p,ncols = y_p)'开始创建空白图,并且以下代码触发错误,如下所示.知道为什么吗?

If I comment out '%matplotlib inline' the code runs fine But if I leave '%matplotlib inline' uncommented, 'fig, axes = plt.subplots(nrows=x_p, ncols=y_p)' starts to create blank plots, and following code triggers error as below. Any idea why?

推荐答案

默认情况下,数字在单元格末尾关闭.这意味着pyplot(plt)忘记了要在下一个单元格中工作的轴.

By default, figures are closed at the end of a cell. This means that pyplot (plt) has forgotten about the axes to work on in the next cell.

%config InlineBackend

告诉我们:

InlineBackend.close_figures = <Bool>
当前:错误
关闭每个单元格末尾的所有图形.
True 时,请确保每个单元格都没有有效数字开头,但也要 意味着必须跟踪参考文献才能进行编辑或重绘 后续单元格中的数字.此模式非常适合笔记本电脑, 来自其他细胞的残留图可能令人惊讶.
如果 False ,则必须调用Figure()来创建新图形.这意味着 gcf()和getfigs()可以引用在其他单元格中创建的图形,并且 活动图可以继续使用pylab/pyplot方法进行编辑, 参考当前的活动图形.

InlineBackend.close_figures= <Bool>
Current: False
Close all figures at the end of each cell.
When True, ensures that each cell starts with no active figures, but it also means that one must keep track of references in order to edit or redraw figures in subsequent cells. This mode is ideal for the notebook, where residual plots from other cells might be surprising.
When False, one must call figure() to create new figures. This means that gcf() and getfigs() can reference figures created in other cells, and the active figure can continue to be edited with pylab/pyplot methods that reference the current active figure.

因此,解决方案是将.close_figures设置为False:

The solution is thus to set .close_figures to False:

%config InlineBackend.close_figures=False

为了防止在活动单元的末尾自动输出活动图形,您可以设置plt.ioff().

In order to prevent the automatic output of active figures at the end of a cell, you may then set plt.ioff().

import matplotlib.pyplot as plt

%matplotlib inline
%config InlineBackend.close_figures=False
plt.ioff()

fig, axes = plt.subplots(ncols=2)

这篇关于'%matplotlib inline'在以下代码中导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆