Matplotlib Plot Numpy Matrix As 0 Index
I prepare a numpy matrix then use matplotlib to plot the matrix, such as: >>> import numpy >>> import matplotlib.pylab as plt >>> m = [[0.0, 1.47, 2.43,
Solution 1:
You can use the extent
optional parameter to the plt.imshow()
function, which is documented here. Like this:
#All the stuff earlier in the program
plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.ocean, extent=(0.5,10.5,0.5,10.5))
plt.colorbar()
plt.show()
For a matrix with an arbitrary shape, you could change this code to something like this:
#All the stuff earlier in the program
plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.ocean,
extent=(0.5,numpy.shape(matrix)[0]+0.5,0.5,numpy.shape(matrix)[1]+0.5))
plt.colorbar()
plt.show()
This produces a plot that looks like this:
Solution 2:
To get the desired output add these lines after your code, but beforeplt.show()
:
...
labels = [0, 1, 3, 5, 7, 9]
ax.set_xticklabels(labels)
plt.show()
Note that x-axis and y-axis are in the range [-0.5, 9.5]
not int [0, 9]
Edit:
To do it in a more flexible way (in fact, another way of the showed above):
labels = range(0, len(m[0]))
plt.xticks(labels)
plt.show()
Output:
Post a Comment for "Matplotlib Plot Numpy Matrix As 0 Index"