15

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, 3.44, 1.08, 2.83, 1.08, 2.13, 2.11, 3.7], [1.47, 0.0, 1.5,     2.39, 2.11, 2.4, 2.11, 1.1, 1.1, 3.21], [2.43, 1.5, 0.0, 1.22, 2.69, 1.33, 3.39, 2.15, 2.12, 1.87], [3.44, 2.39, 1.22, 0.0, 3.45, 2.22, 4.34, 2.54, 3.04, 2.28], [1.08, 2.11, 2.69, 3.45, 0.0, 3.13, 1.76, 2.46, 3.02, 3.85], [2.83, 2.4, 1.33, 2.22, 3.13, 0.0, 3.83, 3.32, 2.73, 0.95], [1.08, 2.11, 3.39, 4.34, 1.76, 3.83, 0.0, 2.47, 2.44, 4.74], [2.13, 1.1, 2.15, 2.54, 2.46, 3.32, 2.47, 0.0, 1.78, 4.01], [2.11, 1.1, 2.12, 3.04, 3.02, 2.73, 2.44, 1.78, 0.0, 3.57], [3.7, 3.21, 1.87, 2.28, 3.85, 0.95, 4.74, 4.01, 3.57, 0.0]]
>>> matrix = numpy.matrix(m)
>>> matrix
matrix([
    [ 0.  ,  1.47,  2.43,  3.44,  1.08,  2.83,  1.08,  2.13,  2.11, 3.7 ],
    [ 1.47,  0.  ,  1.5 ,  2.39,  2.11,  2.4 ,  2.11,  1.1 ,  1.1 , 3.21],
    [ 2.43,  1.5 ,  0.  ,  1.22,  2.69,  1.33,  3.39,  2.15,  2.12, 1.87],
    [ 3.44,  2.39,  1.22,  0.  ,  3.45,  2.22,  4.34,  2.54,  3.04, 2.28],
    [ 1.08,  2.11,  2.69,  3.45,  0.  ,  3.13,  1.76,  2.46,  3.02, 3.85],
    [ 2.83,  2.4 ,  1.33,  2.22,  3.13,  0.  ,  3.83,  3.32,  2.73, 0.95],
    [ 1.08,  2.11,  3.39,  4.34,  1.76,  3.83,  0.  ,  2.47,  2.44, 4.74],
    [ 2.13,  1.1 ,  2.15,  2.54,  2.46,  3.32,  2.47,  0.  ,  1.78, 4.01],
    [ 2.11,  1.1 ,  2.12,  3.04,  3.02,  2.73,  2.44,  1.78,  0.  , 3.57],
    [ 3.7 ,  3.21,  1.87,  2.28,  3.85,  0.95,  4.74,  4.01,  3.57, 0.  ]
])
>>> fig = plt.figure()
>>> ax = fig.add_subplot(1,1,1)
>>> ax.set_aspect('equal')
>>> plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.ocean)
>>> plt.colorbar()
>>> plt.show()

The plot shows like this:

enter image description here

This is fine, except for the fact that I would like my axes to go from 1-10, rather than 0-9 (derived from python's 0 indexing)

Is there a simple way to do this?

Many thanks!!

1
  • Note that x-axis and y-axis are in the range [-0.5, 9.5] Commented Jan 12, 2014 at 4:01

2 Answers 2

12

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:

Plot output

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Are there any advantages to using extent, rather than Christian's method below?
@GarethPrice: I suppose the specific way I have it set up would work better if you had, say, a 1000x1000 matrix, as it lets numpy pick how the tics should be spaced. Other than that they're pretty much the same, and Christian's method wouldn't have to be modified much to make it work like that too.
I think it would always be preferrable to control something like this from the plot command. The other method requires messing around with axis objects and minding when you show etc. If you know in advance that you want the axes a certain way, better to make them right to start with.
2

To get the desired output add these lines after your code, but before plt.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:

enter image description here

1 Comment

Thank you! Likewise with Dan, are there any advantages to doing it this way, rather than using extent??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.