I am trying to plot 1x1 degree data on a matplotlib.Basemap
, and I want to fill the ocean with white. However, in order for the boundaries of the ocean to follow the coastlines drawn by matplotlib
, the resolution of the white ocean mask should be much higher than the resolution of my data.
After searching around for a long time I tried the two possible solutions:
(1) maskoceans()
and is_land()
functions, but since my data is lower resolution than the map drawn by basemap it does not look good on the edges. I do not want to interpolate my data to higher resolution either.
(2) m.drawlsmask()
, but since zorder cannot be assigned the pcolormesh plot always overlays the mask.
This code
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.basemap as bm
#Make data
lon = np.arange(0,360,1)
lat = np.arange(-90,91,1)
data = np.random.rand(len(lat),len(lon))
#Draw map
plt.figure()
m = bm.Basemap(resolution='i',projection='laea', width=1500000, height=2900000, lat_ts=60, lat_0=72, lon_0=319)
m.drawcoastlines(linewidth=1, color='white')
data, lon = bm.addcyclic(data,lon)
x,y = m(*np.meshgrid(lon,lat))
plt.pcolormesh(x,y,data)
plt.savefig('1.png',dpi=300)
Adding m.fillcontinents(color='white')
produces the following image, which is what I need but to fill the ocean and not the land.
Edit:
m.drawmapboundary(fill_color='lightblue')
also fills over land and can therefore not be used.
The desired outcome is that the oceans are white, while what I plotted with plt.pcolormesh(x,y,data)
shows up over the lands.
m.drawmapboundary(fill_color='lightblue')
fills the ocean with a lightblue color. It is not clear why that wouldn't work here. – Arrogantm.drawmapboundary(fill_color='lightblue')
also fills over the land. – Mascagniplt.pcolormesh(x,y,data)
shows up over land only. So the opposite of this: i.sstatic.net/w92pA.png, where the lands are white and what is plotted withplt.pcolormesh(x,y,data)
only shows up over oceans. – Mascagni