views:

27

answers:

1

Hi, I have created an image plot with ax = imshow(). ax is an AxesImage object, but I can't seem to find the function or attribute I need to acess to customize the tick labels. The ordinary pyplots seem to have set_ticks and set_ticklabels methods, but these do not appear to be available for the AxesImage class. Any ideas? Thanks ~

+1  A: 

For what it's worth, you're slightly misunderstanding what imshow() returns, and how matplotlib axes are structured in general...

An AxesImage object is responsible for the image displayed (e.g. colormaps, data, etc), but not the axis that the image resides in. It has no control over things like ticks and tick labels.

What you want to use is the current axis instance.

You can access this with gca(), if you're using the pylab interface, or matplotlib.pyplot.gca if you're accessing things through pyplot. However, if you're using either one, there is an xticks() function to get/set the xtick labels and locations.

For example (using pylab):

import pylab
pylab.figure()
pylab.plot(range(10))
pylab.xticks([2,3,4], ['a','b','c'])
pylab.show()

Using a more object-oriented approach (on a random note, matplotlib's getters and setters get annoying quickly...):

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(1,1,1) # Or we could call plt.gca() later...
im = ax.imshow(np.random.random((10,10)))
ax.set_xticklabels(['a','b','c','d'])  # Or we could use plt.xticks(...)

Hope that clears things up a bit!

Joe Kington
Yup! Thanks - I had expected it to be somewhat similar to matlab where image() returns the axes handle. The name of the object is a bit misleading too... I did notice that about the getter/setter also! In matlab I often pass the arguments to axes setters using the `cellarray{:}` notation which would be like the `*args` and `**kwargs` syntax of python, but in this case it's not quite as straightforward (though I'm sure there are ways around it).
Stephen