Skip to content

Instantly share code, notes, and snippets.

@philippemiron
Created May 4, 2020 20:05
Show Gist options
  • Save philippemiron/cebccfcb199a94ebba1e8bce597ce6a6 to your computer and use it in GitHub Desktop.
Save philippemiron/cebccfcb199a94ebba1e8bce597ce6a6 to your computer and use it in GitHub Desktop.
Problem with cartopy NaturalEarthFeature bathymetry feature
import numpy as np
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
fig = plt.figure(figsize=(10, 4), dpi=300)
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree(), aspect='equal')
ax.set_extent([-80, 10, 35, 80], crs=ccrs.PlateCarree())
# ticks
ax.set_xticks(np.arange(-80, 20, 20), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(40, 100, 20), crs=ccrs.PlateCarree())
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
# add land and coastline and 1000m bathymetry line
ax.add_feature(cfeature.LAND, facecolor='grey', zorder=1)
ax.add_feature(cfeature.COASTLINE, linewidth=0.25, zorder=1)
bathym = cfeature.NaturalEarthFeature(name='bathymetry_J_1000', scale='10m', category='physical')
ax.add_feature(bathym, facecolor='none', edgecolor='black')
@philippemiron
Copy link
Author

I got help on Github.

Here is an updated code.

import numpy as np
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from shapely.ops import cascaded_union

fig = plt.figure(figsize=(10, 4), dpi=300)
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree(), aspect='equal')
ax.set_extent([-100, 10, 0, 90], crs=ccrs.PlateCarree())

# ticks
ax.set_xticks(np.arange(-90, 30, 30), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(40, 100, 20), crs=ccrs.PlateCarree())
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())

# add land and coastline and 1000m bathymetry line
ax.add_feature(cfeature.LAND, facecolor='grey', zorder=1)
ax.add_feature(cfeature.COASTLINE, linewidth=0.25, zorder=1)
bathym = cfeature.NaturalEarthFeature(name='bathymetry_J_1000', scale='10m', category='physical')
bathym = cascaded_union(list(bathym.geometries()))
ax.add_geometries(bathym, facecolor='none', edgecolor='black', linestyle='dashed', linewidth=1, crs=ccrs.PlateCarree())

Screen Shot 2020-06-18 at 19 28 44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment