I have the following code:
import matplotlib.pyplot
import numpy
data_file = open("train/small_train.csv", "r")
data_list = data_file.readlines()
data_file.close()
all_values = data_list[0].split(",")
image_array = numpy.asfarray(all_values[1:]).reshape((28,28))
matplotlib.pyplot.imshow(image_array, cmap="Greys", interpolation="None")
this should read the first line of a .csv file and pick the pixel values (split(","
), put them together to form an image.
The code just runs without any errors but isn't showing the picture...
This should do the trick, you forgot to use the show()
method.
You should use from
keyword to import
only the function you desire. Doing this, you don't need to call the file in which they are (such as matplotlib.pyplot
). I also used the with
keyword that handles very nicely the file director. It opens the file in a clean way, and closes it properly.
from matplotlib import pyplot as plt
import numpy as np
with open("train/small_train.csv", "r") as data:
data_list = data.readlines()
all_values = data_list[0].split(",")
image_array = np.asfarray(all_values[1:]).reshape((28,28))
plt.imshow(image_array, cmap="Greys", interpolation="None")
plt.show()