Custom ticks autoscaled when using imshow?
matplotlib, python
Solution
Also look into using `extent` (doc) to let `matplotlib` do all the thinking about how to put in the tick labels and add in an arbitrary shift:
data = np.array([range(10),range(10,20)])
fig = plt.figure(figsize=(3,5))
ax = fig.add_subplot(111)
ax.imshow(data,aspect='auto',extent=[10000,10010,0,1])
If you definitely want do to it my hand, you might be better off setting the `formatter` and `locator` of the `axis` to get what you want (doc).
import matplotlib.pyplot as plt
import numpy as np
def scale_xaxis(number):
return(number+1001)
def my_form(x,pos):
return '%d'%scale_xaxis(x)
data = np.array([range(10),range(10,20)])
fig = plt.figure(figsize=(3,5))
ax = fig.add_subplot(111)
ax.imshow(data,aspect='auto')
ax.autoscale(False)
ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(int(2)))
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(my_form))
The locator needs to be set to make sure that ticks don't get put at non-integer locations which are then forcible cast to integers by the formatter (which would leave them in the wrong place)
related questions:
matplotlib: format axis offset-values to whole numbers or specific number
removing leading 0 from matplotlib tick label formatting
Problem
I am trying to change the value of the ticks on the x-axis an `imshow` plot using the following code: ``` import matplotlib.pyplot as plt import numpy as np def scale_xaxis(number): return(number+1001) data = np.array([range(10),range(10,20)]) fig = plt.figure(figsize=(3,5)) ax = fig.add_subplot(111) ax.imshow(data,aspect='auto') ax.autoscale(False) xticks = ax.get_xticks() ax.xaxis.set_ticklabels(scale_xaxis(xticks)) plt.savefig("test.png") ``` Resulting image http://ubuntuone.com/2Y5ujtlEkEnrlTcVUxvWLU However the x-ticks overlap and have "non-round" values. Is there some way for matplotlib to automatically do this? Either by using `set_ticklabels` or some other way?