Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib imshow offset to match axis?

I'm plotting a bunch of UTM coordinates using a matplotlib.pyplot.scatter. I also have a background air photo that I know matches the extent of the figure exactly. When I plot my data and set the axis I can display the scatter correctly. If I plot the air photo using imshow it uses the pixel number as the axis location. I need to shift the image (numpy array) to it's correct UTM position. Any ideas? I'm fairly new to matplotlib and numpy.

For example: I know that the top left corner of the image (imshow coordinate: 0,0) has the UTM coordinate (269658.4, 538318.2). How do I tell imshow the same thing?

I should also say that I investigated Basemap but it doesn't appear to fully support UTM yet. My study area is very small.

like image 360
cds-mewald Avatar asked Sep 07 '12 19:09

cds-mewald


People also ask

Does Imshow normalize?

By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).

What is interpolation in Imshow?

This example displays the difference between interpolation methods for imshow . If interpolation is None, it defaults to the rcParams["image. interpolation"] (default: 'antialiased' ). If the interpolation is 'none' , then no interpolation is performed for the Agg, ps and pdf backends.

What is extent in Imshow Python?

Extent defines the left and right limits, and the bottom and top limits. It takes four values like so: extent=[horizontal_min,horizontal_max,vertical_min,vertical_max] .


1 Answers

You need to use the extent keyword argument to imshow.

As a quick example:

import numpy as np
import matplotlib.pyplot as plt

# Random points between 50000 and 51000
x, y = 1000 * np.random.random((2, 10)) + 50000

# A 10x10 "image"...
image = np.arange(100).reshape((10,10))

# In a lot of cases, image data will be "flipped" vertically, so you may need 
# use the `origin` kwarg, as well (or just swap the ymin and ymax ordering).
plt.imshow(image, extent=[x.min(), x.max(), y.min(), y.max()])
plt.plot(x, y, 'ro')

plt.show()

enter image description here

like image 149
Joe Kington Avatar answered Oct 07 '22 00:10

Joe Kington