con-f-use
con-f-use

Reputation: 4028

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.

Upvotes: 4

Views: 6865

Answers (2)

jmetz
jmetz

Reputation: 12773

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

Upvotes: 5

Chiel
Chiel

Reputation: 6194

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

Upvotes: 4

Related Questions