Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use 'extent' in matplotlib.pyplot.imshow

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

like image 723
Chad Avatar asked Aug 09 '11 16:08

Chad


People also ask

What is extent in Imshow?

The extent keyword arguments controls the bounding box in data coordinates that the image will fill specified as (left, right, bottom, top) in data coordinates, the origin keyword argument controls how the image fills that bounding box, and the orientation in the final rendered image is also affected by the axes limits ...

What is extent in Python?

Extent defines the left and right limits, and the bottom and top limits.

How do you scale Imshow?

Use the extent parameter of imshow to map the image buffer pixel coordinates to a data space coordinate system. Next, set the aspect ratio of the image manually by supplying a value such as "aspect=4" or let it auto-scale by using aspect='auto'. This will prevent stretching of the image.

How does Matplotlib Imshow work?

The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .


1 Answers

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.

like image 149
Yann Avatar answered Sep 20 '22 23:09

Yann