My question is the same as this one: Fill oceans in basemap. I know there is an answer given here, but when I try to apply the answer to my code, it doesn't work.
Here is my original graph.
I want to make the oceans white and only show the colour differences on Antarctica.
Here is the code that I tried:
from mpl_toolkits.basemap import Basemap
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Path, PathPatch
D = pd.read_pickle('directory')
fig, ax = plt.subplots()
lons = D['longitude'][:]
lats = D['latitude'][:]
lon, lat = np.meshgrid(lons, lats)
variable_to_graph = D['variable_to_graph']
m = Basemap(projection='spstere', boundinglat=-60, lon_0=180, resolution='l')
m.drawcoastlines(color='k') # shows Antarctica
x,y = m(lon, lat)
ax.pcolormesh(x, y, variable_to_graph, cmap='viridis', vmin=-400, vmax=-0)
# Getting the limits of the map:
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
map_edges = np.array([[x0,y0], [x1,y0], [x1,y1], [x0,y1]])
# Getting all polygons used to draw the coastlines of the map
polys = [p.boundary for p in m.landpolygons]
# Combining with map edges.
polys = [map_edges]+polys[:]
# Creating a PathPatch.
codes = [
[Path.MOVETO] + [Path.LINETO for p in p[1:]]
for p in polys
]
polys_lin = [v for p in polys for v in p]
codes_lin = [c for cs in codes for c in cs]
path = Path(polys_lin, codes_lin)
patch = PathPatch(path,facecolor='white', lw=0)
# Masking the data:
ax.add_patch(patch)
plt.show()
Which gives me this.
I have also tried using a is_land() function but as the resolution is relatively low (1x1 degrees) the image is not very neat.
Any help would be appreciated as to how to get the colour map to only show on Antarctica and for it to stop on the edges of Antarctica.
EDIT: I have updated my question with code that can be tested.
from mpl_toolkits.basemap import Basemap
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Path, PathPatch
# D = pd.read_pickle('directory')
fig, ax = plt.subplots()
lons = range(0,361)
lats = [-60, -61, -62, -63, -64, -65, -66, -67, -68, -69, -70,
-71, -72, -73, -74, -75, -76, -77, -78, -79, -80, -81,
-82, -83, -84, -85, -86, -87, -88, -89, -90]
lon, lat = np.meshgrid(lons, lats)
variable_to_graph = np.random.rand(31,361)
m = Basemap(projection='spstere', boundinglat=-60, lon_0=180, resolution='l')
m.drawcoastlines(color='k') # shows Antarctica
x,y = m(lon, lat)
ax.pcolormesh(x, y, variable_to_graph, cmap='viridis', vmin=-400, vmax=-0)
# Getting the limits of the map:
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
map_edges = np.array([[x0,y0], [x1,y0], [x1,y1], [x0,y1]])
# Getting all polygons used to draw the coastlines of the map
polys = [p.boundary for p in m.landpolygons]
# Combining with map edges.
polys = [map_edges]+polys[:]
# Creating a PathPatch.
codes = [
[Path.MOVETO] + [Path.LINETO for p in p[1:]]
for p in polys
]
polys_lin = [v for p in polys for v in p]
codes_lin = [c for cs in codes for c in cs]
path = Path(polys_lin, codes_lin)
patch = PathPatch(path,facecolor='white', lw=0)
# Masking the data:
ax.add_patch(patch)
plt.show()