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])
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:
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?
The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .
show() displays the figure (and enters the main loop of whatever gui backend you're using). You shouldn't call it until you've plotted things and want to see them displayed. plt. imshow() draws an image on the current figure (creating a figure if there isn't a current figure).
Plotting from an IPython shell draw() . Using plt. show() in Matplotlib mode is not required.
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
just finishes drawing a picture instead of printing it. If you want to print the picture, you just need to add plt.show
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With