Jupyter Notebook 中无法显示 matplotlib 图片

25

我正在使用Ubuntu 14.04,在Jupyter Notebook中编码,使用Anaconda2.7,其他所有内容都是最新的。今天我在编码时一切都正常。我关闭笔记本电脑,重新打开它时,一切都正常,只是图像无法显示。

%matplotlib inline
import numpy as np
import skimage
from skimage import data
from matplotlib import pyplot as plt
%pylab inline

img = data.camera()
plt.imshow(img,cmap='gray')

这是我正在使用的代码,非常简单,但不显示图像。

<matplotlib.image.AxesImage at 0xaf7017ac>

这段内容会显示在输出区域,请帮忙。

4个回答

37
你需要告诉Matplotlib实际显示图像。在你的代码段末尾添加以下内容:
plt.show()

16

在Jupyter Notebook中使用matplotlib展示图片,需要使用%matplotlib inline魔法命令以及plt.show()代码。
至于您的代码,将plt.imshow()表达式后添加plt.show()代码即可显示图片。


11

如果使用 inline 后端,只需调用 plt.show()

如果您使用 notebook 后端(%matplotlib notebook),则应在 plt.imshow(img) 之前调用 plt.figure()。如果您希望使用交互式图形,则特别重要!


1
将内核从xpython更改为原始的Python内核。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接