2

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.

Original plot

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.

Result

I have also tried using a is_land() function but as the resolution is relatively low (1x1 degrees) the image is not very neat.

Fill result

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()
chaos
  • 21
  • 3
  • 1
    Basemap was essentially discontinued about 3 years ago... Have you tried using [`cartopy`](https://scitools.org.uk/cartopy/docs/v0.15/gallery.html)? I'll take a look when I get a chance, but if you post again, try to include data (or fake data) so people can reproduce your problem. – Matt Hall Nov 20 '19 at 16:48
  • I've realised there is a problem with the m.landpolygons command. The length of the list I get is 0, where as in the Greenland example it is 73. So basically m.landpolygons doesn't work for some reason. – chaos Nov 21 '19 at 10:40
  • I have now produced a reproducible example. Thanks! – chaos Nov 21 '19 at 13:39

0 Answers0