-1

I have a 2D array and it's contents will display correctly as an image when I simply use

img = plt.imshow(full2DArray)

but my problem is that the axes just naively show the number of rows and columns. For example if my 2D array is 53x53 then the axes will count 0-53 on the y-axis and 0-53 on the x-axis.

I need to show the exact same image but have the axes display a linear scale from -130 to +130 instead.

Joe Soap
  • 19
  • 5

2 Answers2

1

If I understand it correctly, you need predifined axis, instead of pyplot infering these from the image.

Setting xlim before calling imshow will do the job.

plt.xlim([-130, 130])

Similarly, you can call ylim for the y axis.

Stackerexp
  • 495
  • 4
  • 12
1

I have a similar answer to this question here but to explain for your case, we can take an array data = np.random.rand(53,53) filled with random values, and plot it with imshow. You simply need to adjust the extent=[<xmin>,<xmax>,<ymin>,<ymax>] parameter, so in the example code:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(53,53)
print(data.shape) # Displays (53,53)

plt.figure()
plt.xlabel("x")
plt.ylabel("y")
plt.imshow(data, origin='lower', aspect='auto',
 extent = [-130,130,-130,130], cmap=plt.cm.jet)
plt.colorbar()
plt.show()

We get the following plot with your desired bounds:

test

t.o.
  • 832
  • 6
  • 20