问题描述
我是 keras 的新手,当我尝试在我的 linux 上运行我的第一个 keras 程序时,有些事情并没有如我所愿.这是我的python代码:
I am a newbie to keras, and when I tried to run my first keras program on my linux, something just didn't go as I wish.Here is my python code:
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的后端没有任何问题.我已经通过下面的代码测试过了.
The last sentence doesn't display anything. I copied those codes from a tutorial with out any modification. And there is nothing wrong with the backend of matplotlib on my computer. I have tested that through the code below.
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()
然后我得到了这样的图像:
And then I got a image like that:
此外,我可以打印 X_train[0] 并且似乎没有错.
那么这可能是什么原因呢?为什么我的第一个代码中的 imshow() 函数没有显示任何内容?
Moreover, I can get X_train[0] printed and it seems nothing wrong.
So what could be the reason for that? Why the imshow() function in my first code didn't display anything?
推荐答案
解决方案就像在代码段末尾添加 plt.show()
一样简单:
The solution was as simple as adding plt.show()
at the end of the code snippet:
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()
这篇关于为什么 plt.imshow() 不显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!