792

how to display a image over a map with imshow?

Question:

I have a geotif with 9 different colour values (0-9) and want to display it over a map. I'm trying to use it with basemap from the matplotlib package.

from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt im = plt.imread('a.tif') extent = [-18, 52, -38, 38] # [left, right, bottom, top] m = Basemap(projection='merc', llcrnrlon=extent[0], urcrnrlon=extent[1], llcrnrlat=extent[2], urcrnrlat=extent[3], resolution='c') m.drawcountries() plt.imshow(im, extent=extent, alpha=0.6) plt.show()

I only get a black and white image. When I uncomment drawcountries() I see the data from the tif.

How can I draw the colors of the tif on a map and add there country borders?

Answer1:

m.imshow(im, extent=extent, alpha=0.6)

Answer2:

Using extent does not solve your problem! It creates a bigger problem because it looks to be ok but it is wrong.

The extent will work only for cylindrical projections and keeping distances. It just adjusts the image to some box, linearly! The mercator projection is not linear.

Try to use the projection 'cyl'. So, the answer is: there is no way for nonlinear transformations. The possible workaround is to convert the image coordinates to distances in the new projection and then 2d interpolation (matplotlib.tri, for instance) to some new rectangle (of distances).

Recommend