Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customize ticks for AxesImage?

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 ~

like image 367
hatmatrix Avatar asked Sep 15 '10 09:09

hatmatrix


1 Answers

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!

like image 55
Joe Kington Avatar answered Sep 23 '22 19:09

Joe Kington