Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 227
con-f-use Avatar asked Jan 05 '23 22:01

con-f-use


2 Answers

Generally for any plot you can use xticks and yticks, e.g.

import numpy as np
import matplotlib.pyplot as plt

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

plt.imshow(data, interpolation='none')
plt.xticks([0, 1, 2, 3, 4], [10,20,30,40,50])
plt.yticks([0, 1, 2, 3, 4], [10,20,30,40,50])
plt.show()

enter image description here

like image 173
jmetz Avatar answered Jan 12 '23 10:01

jmetz


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

like image 23
Chiel Avatar answered Jan 12 '23 11:01

Chiel