Search code examples
pythonmatplotlibscale

How can I rescale axis without scaling the image in an image plot with matplotlib?


I have a simple image plot like this:

import numpy as np
import matplotlib.pyplot as plt

data = np.arange(25)
data.shape = (5,5)

plt.imshow(data, interpolation='none')
plt.show()

In this case the input data a 5x5 matrix. The ticks on the axis go from 0 to 4. How would I go about changing this range to say 10 to 50 without changing the displayed image? I want to rescale the axis without scaling the image.


Solution

  • With the extent property of imshow you can put any possible range on the axis, without changing the plot.

    import numpy as np
    import matplotlib.pyplot as plt
    
    data = np.arange(25)
    data.shape = (5,5)
    
    plt.imshow(data, interpolation='none', extent=(-3, 27, 5, 31))
    plt.show()
    

    This gives:

    enter image description here