ChatGPT解决这个技术问题 Extra ChatGPT

为什么 plt.imshow() 不显示图像?

我是 keras 的新手,当我尝试在我的 linux 上运行我的第一个 keras 程序时,事情并没有如我所愿。这是我的python代码:

import numpy as np
np.random.seed(123)
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
(X_train,y_train),(X_test,y_test) = mnist.load_data()
print X_train.shape
from matplotlib import pyplot as plt
plt.imshow(X_train[0])

最后一句没有显示任何内容。我从教程中复制了这些代码,没有进行任何修改。而且我电脑上matplotlib的后端也没有问题。我已经通过下面的代码进行了测试。

import matplotlib.pyplot as plt

data = [[0, 0.25], [0.5, 0.75]]

fig, ax = plt.subplots()
im = ax.imshow(data, cmap=plt.get_cmap('hot'), interpolation='nearest',
               vmin=0, vmax=1)
fig.colorbar(im)
plt.show()

https://i.stack.imgur.com/2r6Lw.png

此外,我可以打印 X_train[0] ,这似乎没有错。那么这可能是什么原因呢?为什么我的第一个代码中的 imshow() 函数没有显示任何内容?

尝试在代码段末尾添加 plt.show()
当您从 Jupyter 复制代码时,经常会出现此问题

M
Marcin Możejko

解决方案很简单,只需在代码段末尾添加 plt.show()

import numpy as np
np.random.seed(123)
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
(X_train,y_train),(X_test,y_test) = mnist.load_data()
print X_train.shape
from matplotlib import pyplot as plt
plt.imshow(X_train[0])
plt.show()

这只是避免在获得 168 票的同时回答为什么......奇怪
2021 年 3 月。教程中仍然缺少相同的内容tensorflow.org/tutorials/images/transfer_learning
s
shaik moeed

plt.imshow 只是完成了图片的绘制,而不是打印出来。如果要打印图片,只需添加plt.show


从这个角度来看,将“imshow”重命名为“imdraw”会更好,也不会令人困惑。你不同意吗?
@AramParonikyan matplotlib 是层层叠叠的混乱和地狱般的设计。有一天人们会搬到一个更好的图书馆(cv2 更令人困惑)
W
WestCoastProjects

plt.imshow 在坐标轴上显示图像,但如果您需要显示多个图像,请使用 show() 来完成图。下一个示例显示了两个数字:

import numpy as np
from keras.datasets import mnist
(X_train,y_train),(X_test,y_test) = mnist.load_data()
from matplotlib import pyplot as plt
plt.imshow(X_train[0])
plt.show()
plt.imshow(X_train[1])
plt.show()

在 Google Colab 中,如果您将前面示例中的 show() 方法注释掉,则只会显示一个图像(后面的一个与 X_train[1] 连接)。

以下是帮助中的内容:

plt.show(*args, **kw)
        Display a figure.
        When running in ipython with its pylab mode, display all
        figures and return to the ipython prompt.

        In non-interactive mode, display all figures and block until
        the figures have been closed; in interactive mode it has no
        effect unless figures were created prior to a change from
        non-interactive to interactive mode (not recommended).  In
        that case it displays the figures but does not block.

        A single experimental keyword argument, *block*, may be
        set to True or False to override the blocking behavior
        described above.



plt.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, hold=None, data=None, **kwargs)
        Display an image on the axes.

Parameters
----------
X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4)
    Display the image in `X` to current axes.  `X` may be an
    array or a PIL image. If `X` is an array, it
    can have the following shapes and types:

    - MxN -- values to be mapped (float or int)
    - MxNx3 -- RGB (float or uint8)
    - MxNx4 -- RGBA (float or uint8)

    The value for each component of MxNx3 and MxNx4 float arrays
    should be in the range 0.0 to 1.0. MxN arrays are mapped
    to colors based on the `norm` (mapping scalar to scalar)
    and the `cmap` (mapping the normed scalar to a color).

R
Rajat

如果要使用 imshow() 打印图片,还可以执行 plt.show()


措辞令人困惑。对于答案,细节和解释似乎相当薄弱。能否请您edit详细说明。它目前似乎是在写它的过程中发布的东西......
这与上面 3 个答案中已经说过的有什么不同?