0

I have a python plot, using the following code:

fig = plt.figure()
ax = plt.add_subplot(111)
ax.imshow(an_image)

What I want to do, is simply force the x and y axes, to span a certain minimum and maximum number. (Cannot do this with ax.set_xlim() or ax.set_ylim() because those actually change the axis limits). I just want some particular x_min , x_max , y_min, and y_max numbers to show up on my image.

How do I do that?

EDIT: To further clarify what I mean, look at this image below. What I want, are the numbers on the axes written in red. I want them to replace what is already there in black. How do I accomplish that?

enter image description here

Thanks

Spacey
  • 2,941
  • 10
  • 47
  • 63

2 Answers2

1

You can specifically set tick locations with:

ax.set_xticks([0, 200])
ax.set_yticks([0, 100])

ax.set_xticklabels(['-30', '+100'])
ax.set_yticklabels(['+20', '-20'])

Alternatively, you could change the origin to lower with imshow and you wouldn't have to flip the yticklabels.

GWW
  • 43,129
  • 11
  • 115
  • 108
  • Hmm, ok but when I go that, they all seem to be smooshed to the side. Does that make sense? So let's say I have an image that spans 100x200. I literally just want say, the xaxis to show it spanning from say, -54 to 453, for example, at some interval that I specify. Does that make sense? – Spacey Jun 06 '17 at 00:05
  • '['-30', '+100']' is a list of strings. How then do I make a list of numbers, into a list of strings? I want to use the np.arange(...).tolist() command , but it seems that I need to then make that list into a list of strings. How do I do that? Thanks – Spacey Jun 06 '17 at 01:05
  • I believe you can give `set_xticklabels` integers. I used strings so that the `+` is retained on the axes. If you can't you can always use `map(str, NP.arange(...).tolist())`. You can do other fancy things with map if you need to specifically format the integers. For example, `map('{0:+d}'.format, NP.arange(..))` will prepend `+` or `-` to the values. – GWW Jun 06 '17 at 01:22
  • This solution still doesn't work. It just "maxes out" at the first two numbers. – Spacey Jun 06 '17 at 02:04
  • For example, saying `ax.set_xticklabels(['-10', '10', '30'])` just shows -10 and 10, and doesnt show 30 anywhere. – Spacey Jun 06 '17 at 02:05
  • `set_xticks` sets the position, while `set_xticklabels` sets the text that appears at that position. So you need to have an additional `xtick` to go with the extra label – GWW Jun 06 '17 at 02:18
1

You may consider using the image extent to set the actual scale of the image. I.e. to let your image scale from -30to 100 in x- and -20 to 20 you can use

ax.imshow(data, origin="lower", extent=[-30, 100, -20,20])

An example:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.normal(size=(10,20))
xmin, xmax = -30, 100
ymin, ymax = -20, 20


fig, ax = plt.subplots()

extent= [xmin, xmax, ymin, ymax]
ax.imshow(data, origin="lower", extent=extent)

ax.set_xticks(np.arange(xmin,xmax+1,10))
ax.set_yticks([-20,0,20])
plt.show()

enter image description here

Note that this might scale the image pixels to be non-square. To preserve square pixels, you may use the aspect argument.

extent= [xmin, xmax, ymin, ymax]
aspect=data.shape[0]/float(ymax-ymin)/(data.shape[1]/float(xmax-xmin))
ax.imshow(data, origin="lower", extent=extent, aspect=aspect)

enter image description here

You might also want to look at this question: Matplotlib: how to make imshow read x,y coordinates from other numpy arrays? for how to put the ticks to the pixel centers, which makes sense depending on the actual meaning of the data.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712