0

Using matplotlib.pyplot I want to plot my data relative to a certain set of axes but then I want to have my axis show a different range of values without affecting the portion of my data that is shown.

To be specific, I have a 1000x1000 grid of data I am plotting with pyplot.imshow(); however, I only want my axes to run from -2 to 2 (both x and y axes) while still showing the whole 1000x1000 grid. If possible, I would like to hard code these values (-2 and 2) as being the limits of my axis rather than using some sort of rescaling from the 1000x1000 grid to the new axes.

Here is my code:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

array = np.loadtxt("output.dat",unpack=True)
im = plt.imshow(array,cmap='hot')
plt.colorbar(im)
NeutronStar
  • 2,057
  • 7
  • 31
  • 49
  • this question might be of use to you: http://stackoverflow.com/questions/3677368/matplotlib-format-axis-offset-values-to-whole-numbers-or-specific-number – ajdigregorio Feb 20 '15 at 16:53
  • @paintedcones that helps but what I would like to do is basically hard code the axes to values I set rather than using some sort of axis rescaler like I see in the examples in the link you posted. – NeutronStar Feb 20 '15 at 17:10
  • Have you tried `plt.ylim(-2,2)` `plt.xlim(-2,2)`? – N1B4 Feb 20 '15 at 17:20
  • @N1B4 That zooms in on that portion of the grid rather than scaling the axes while still showing the whole grid. – NeutronStar Feb 20 '15 at 17:23
  • Do you just want to scale all values in the gridded data to be within 2 to -2? That's just normalizing the data to (-1,1) and then adding 1 to all grid points. – N1B4 Feb 20 '15 at 18:31
  • @N1B4, that's not what I'm trying to do. I want the spatial extent of the grid to be within 2 and -2, not the values of the grid itself. – NeutronStar Feb 20 '15 at 21:18

1 Answers1

1

Just found an answer which works specifically for plt.imshow(). The extent parameter. Replacing

im = plt.imshow(array,cmap='hot')

in my original code example with

im = plt.imshow(array,cmap='hot',extent=(-2,2,-2,2))

will take care of things.

NeutronStar
  • 2,057
  • 7
  • 31
  • 49
  • @paintedcones It does work. If you look, the question asker and the answerer are the same person. I wouldn't have posted an answer to my own question if the answer didn't work! – NeutronStar Feb 20 '15 at 21:14
  • Yes, I know.. but if this is the correct answer, you should press "accept answer" so future SO visitors know. – ajdigregorio Feb 20 '15 at 21:16
  • 1
    @paintedcones, when you answer your own question you can't accept the answer for two days. – NeutronStar Feb 20 '15 at 21:19