1

I have an meshplot onto which I want to overlay continents in basemap. I am using this code:

       from mpl_toolkits.basemap import Basemap
       import matplotlib.pyplot as plt
       m = Basemap(width=12000000,height=9000000,projection='lcc',
            resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)

       m.drawlsmask(land_color='coral',ocean_color='aqua',lakes=True)
       plt.show()

referring to this, my requirements is opposite. I want continents ontop of the meshplot or image I have so only mesh at the ocean area is visible.

gfdsal
  • 651
  • 1
  • 8
  • 25

1 Answers1

2

Your requirements:

  1. A plot with continents on top of a meshplot and/or image
  2. Only mesh/image at the ocean area is visible

To get the plot, you must use 'zorder' in each of the layers involved. The data to plot must be transformed appropriately. Here is a code that you can try, and the output plot it produces.

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

m = Basemap(projection='lcc', width=12000000, height=9000000, 
    resolution='c', lat_1=45., lat_2=55, lat_0=50, lon_0=-107.)

# if resolution is None, coastlines wont draw

# draw only land areas with zorder=20
# any other layers with zorder below 20 will be hidden by land areas
m.drawlsmask(land_color='coral', ocean_color='none', lakes=True, zorder=20)
m.drawcoastlines(linewidth=0.3, color='gray', zorder=25)

filename = "small_01.png"  #use your image here
lonmin, lonmax, latmin, latmax = (-130, -40, 35, 45) # set limits of the image

# compute the limits of the image in data coordinates
left, bottom = m (lonmin, latmin)
top, right = m(lonmax, latmax)
image_extent = (left, right, bottom, top)

ax = plt.gca()
# set zorder < 20, to plot the image below land areas
ax.imshow(plt.imread(filename), extent=image_extent, zorder=15)

# plot some meshgrid data
# set zorder above image, but below land
xs = np.linspace(-130, -60, 20)
ys = np.linspace(20, 60, 10)
x2d, y2d = np.meshgrid(xs, ys)

#ax.plot(*m(x2d, y2d), 'ro', zorder=16)  # faster
ax.scatter(*m(x2d, y2d), s=2, zorder=16)

plt.show()

enter image description here

Edit 1

Some useful code snippet:

# This plots shaded relief terrain covering land and sea.
m.shadedrelief(zorder = 25)

# This plots only ocean/sea parts on top.
m.drawlsmask(land_color='none', ocean_color='aqua', zorder=26)

enter image description here

swatchai
  • 17,400
  • 3
  • 39
  • 58
  • This is exactly what I needed. So when you set ocean color to 'none', it removes the ocean right? So anything underneath can be viewed by zorder. – gfdsal Jan 23 '20 at 10:01
  • Also one more thing, can the land color be changed to shaded relief instead of plain color? Using ```m.shadedrelief()``` simply covers everything. – gfdsal Jan 23 '20 at 10:12
  • 1
    @GENIVI-LEARNER Yes. See edited answer and sample plot. – swatchai Jan 23 '20 at 11:11
  • Perfect. Thats all I needed! – gfdsal Jan 23 '20 at 11:52
  • please [check this](https://stackoverflow.com/questions/59887138/matplotlib-basemap-compositing) if convenient – gfdsal Jan 23 '20 at 21:15