40

I managed to plot my data and would like to add a background image (map) to it. Data is plotted by the long/lat values and I have the long/lat values for the image's three corners (top left, top right and bottom left) too.

I am trying to figure out how to use 'extent' option with imshow. However, the examples I found don't explain how to assign x and y for each corner ( in my case I have the information for three corners).

How can I assign the location of three corners for the image when adding it to the plot?

Thanks

1

2 Answers 2

55

Specify, in the coordinates of your current axis, the corners of the rectangle that you want the image to be pasted over

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].

Assuming you have longitude along the horizontal axis, then use extent=[longitude_top_left,longitude_top_right,latitude_bottom_left,latitude_top_left]. longitude_top_left and longitude_bottom_left should be the same, latitude_top_left and latitude_top_right should be the same, and the values within these pairs are interchangeable.

If your first element of your image should be plotted in the lower left, then use the origin='lower' imshow option as well, otherwise the 'upper' default is what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

Still not clear. What do you mean by "max of the horizontal values"? There are no values that are horizontal: there is a 2d matrix. Do you mean the maximum in each row? But there is a bunch of them. The maximum over those then? But then the horizontal and vertical are the same.
The max is the number written at the right edge of the matrix in the plot, the min is the number at the left edge.
12

Here's an example based on http://matplotlib.org/examples/pylab_examples/image_demo3.html showing use of extent.

#!/usr/bin/env python
from pylab import *
try:
    from PIL import Image
except ImportError, exc:
    raise SystemExit("PIL must be installed to run this example")

import matplotlib.cbook as cbook

datafile = cbook.get_sample_data('ada.png')
h = Image.open(datafile)
dpi = rcParams['figure.dpi']
figsize = h.size[0]/dpi, h.size[1]/dpi

figure(figsize=figsize)
ax = axes([0,0,1,1], frameon=False)
ax.set_axis_off()
ax.set_xlim(0,2)
ax.set_ylim(0,2)
im = imshow(h, origin='upper',extent=[-2,4,-2,4])  # axes zoom in on portion of image
im2 = imshow(h, origin='upper',extent=[0,.5,0,.5]) # image is a small inset on axes

show()

If you don't set your axis limits, they become your extents & then don't seem to have any effect.

Ada Lovelace image with inset

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.