简体   繁体   English

在ipython中使用matplotlib显示图像

[英]Displaying image with matplotlib in ipython

How can I display an image imported with numpy package using matplotlib in ipython? 如何在ipython中使用matplotlib显示使用numpy包导入的图像?

It should be fairly easy with the command 使用该命令应该相当容易

import numpy as np
import matplotlib.pyplot as plt

im = np.array(Image.open('image.jpg'))

plt.imshow(im)

But the image does not show and I just get the output 但图像没有显示,我只是得到输出

<matplotlib.image.AxesImage at 0x7fb38e10ff10>

You must call plt.show() to actually bring up the windows. 你必须调用plt.show()来实际调出窗口。 You can get around this by using interactive mode . 您可以使用交互模式解决这个问题。 But for scripts it is better to simply call show() after completing all your plotting commands. 但对于脚本,最好在完成所有绘图命令后调用show()

In IPython or Jupyter notebooks, if you want to show images as inline in the notebook and not in a separate window, implement the code shown below. 在IPython或Jupyter笔记本中,如果要在笔记本中将图像显示为内联而不是在单独的窗口中,请实现下面显示的代码。

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

im = np.array(Image.open('image.jpg'))

plt.imshow(im)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM