python - Make Matplotlib map plots line up with each other -
i'm trying produce figure in python display (among other things):
a) basemap transformed mercator projection image
b) labelled gridlines
i figure in transverse mercator (or other spherical) projection.
i have tried both matplotlib basemap , cartopy. cartopy can (a), , basemap can (b), cartopy can label gridlines on platecarree plots, , basemap not support transformation of images using imshow()
.
unless can suggest alternative, think simplest way around overlay gridlines , labels basemap plot on reprojected image. cannot 2 plots line each other. have far:
import numpy np import matplotlib.pyplot plt mpl_toolkits.basemap import basemap import cartopy.crs ccrs #setup figure fig = plt.figure(figsize=(20, 20)) #set figure limits (lat long) xlimits = [25, 42] ylimits = [25, 40] #where projection centred centre = [33, 33] #image limits in mercator eastings , northings imxlimits = [25, 42] imylimits = [25, 40] #transform image limits imextent = tuple(ccrs.mercator().transform_points(ccrs.geodetic(), np.array(imxlimits), np.array(imylimits))[:, 0:2].t.flatten()) #load image image = plt.imread(dir + 'topo.png') tm = ccrs.transversemercator(central_longitude=centre[0], central_latitude=centre[1]) ll = ccrs.geodetic() #setup image axies ax = fig.add_subplot(111, projection=tm) ax.set_extent(xlimits + ylimits, ll) #<--limits defined here #plot image transformed ax.imshow(image, origin='upper', extent=imextent, transform=ccrs.mercator()) #create axes gridlines axl = fig.add_subplot(111) #make figure background transparent axl.patch.set_alpha(0) #make basemap instance m = basemap(projection='tmerc', resolution='h', ax=axl, lat_0=centre[1], lon_0=centre[0], llcrnrlon=xlimits[0], llcrnrlat=ylimits[0], #<--limits defined here urcrnrlon=xlimits[1], urcrnrlat=ylimits[1]) m.drawcoastlines() #to check if images match #draw gridlines m.drawparallels(np.arange(20, 50), labels=[false, true, false, false]) m.drawmeridians(np.arange(20, 50), labels=[false, false, false, true]) plt.show()
this produces plots on top of each other missmatch. think because limits given first plot might set top , bottom edge, , (same) limits given second plot top right , bottom left corners.
any tips on how fix this?
thanks!
so managed work replacing how cartopy limits defined, instead of:
ax.set_extent(xlimits + ylimits, ll)
i first converted transverse mercator coordinates:
tmlims = tm.transform_points(ll, np.array(xlimits), np.array(ylimits))
and set extent this:
ax.set_extent([tmlims[0][0], tmlims[1][0], tmlims[0][1], tmlims[1][1]], tm)
as plot in transverse mercator coordinates don't know why doing conversion manually made difference (as defining ll
rather tm
should same conversion). maybe i'm misunderstanding how conversion of limits.
on way finding solution found out these bits of code might people other 'maps not lining up' problems:
ax.get_extent(crs) #return extents of cartopy axies in given co-ordinates # #these work matplotlib axies ax.set_anchor('w') #anchor plot left (in case) of canvas ax.set_aspect('equal') #distort (or not) aspects of plot
see http://matplotlib.org/api/axes_api.html , http://scitools.org.uk/cartopy/docs/v0.4/matplotlib/geoaxes.html
Comments
Post a Comment