Jupyter noteb中的Imagegrid

2024-04-23 22:24:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循Imagegrid上matplotlib文档中的一个示例,并尝试从Jupyter notebook中复制它:

% matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np

im = np.arange(100)
im.shape = 10, 10

fig = plt.figure(1, (4., 4.))
grid = ImageGrid(fig, 111,  # similar to subplot(111)
                 nrows_ncols=(2, 2),  # creates 2x2 grid of axes
                 axes_pad=0.1,  # pad between axes in inch.
                 )

for i in range(4):
    grid[i].imshow(im)  # The AxesGrid object work as a list of axes.


plt.show()

预期产量:

enter image description here

我得到的是:

enter image description here

我没有得到图像的网格,如你所见。我做错什么了?在

编辑 如果我删除%matplotlib inline选项,我就得到这个(它是cell[1]来证明我重新启动了内核):

enter image description here

未显示绘图。在

我正在运行matplotlib版本3.0.0,用conda list matplotlib检查,jupyter是{},检查的是{}。在Windows10、Anaconda、Python3.6上。在


Tags: ofinimportmatplotlibasnpfiginline
2条回答

这是一个issue with matplotlib 3.0.0。这现在是been fixed,因此它不会出现在即将到来的3.0.1错误修复版本中。在

同时你有两个选择。在

  1. 恢复为matplotlib 2.2.3
  2. 决定在使用%matplotlib inline时不裁剪图像。请通过

    %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
    

    在IPython或Jupyter。

删除

%matplotlib inline

然后重新启动一切或者把它放在一个单独的牢房里,如下所示。magic命令似乎总是需要在绘图之前在单独的单元中运行,如果它是在需要重新启动内核之前运行的。看这里 enter link description here

enter image description here

它会起作用的。%matplotlib inline不需要在jupyter中呈现绘图,这只是一种方便。表演()将在调用时呈现绘图。在

我在jupyter遇到过这个问题。我认为问题在于,magic命令会使它在任何绘图可用时立即呈现,而mpl则会等待,直到被告知要渲染以及如何渲染。在

完整的示例代码来自您在问题中链接的mpl示例:

^{pr2}$

enter image description here

相关问题 更多 >