Skip to content

Instantly share code, notes, and snippets.

@aidanheerdegen
Last active May 14, 2018 00:41
Show Gist options
  • Save aidanheerdegen/4968b94768bf732272c7ba078c154d50 to your computer and use it in GitHub Desktop.
Save aidanheerdegen/4968b94768bf732272c7ba078c154d50 to your computer and use it in GitHub Desktop.
Script to show problems using the cartopy ocean feature to mask out the ocean
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mpc
import matplotlib as mpl
import cartopy.crs as ccrs
import cartopy.feature as cpf
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
# set the size limits
yrg = 2160
xrg = 4320
# make an array
image = np.arange((yrg*xrg)).reshape((yrg, xrg))
# Setup the figure
fig, ax = plt.subplots(1, 1, figsize=(12,9),
subplot_kw={'projection': ccrs.PlateCarree()})
ax.set_global()
# make the map
im = ax.imshow(image, extent=[-180, 180, 90, -90])
ax.add_feature(cpf.OCEAN, facecolor="w", alpha=1, zorder=100)
ax.add_feature(cpf.COASTLINE, zorder=101)
ax.add_feature(cpf.BORDERS, linestyle='--', zorder=102)
ax.add_feature(cpf.LAKES, alpha=0.5, zorder=103)
ax.add_feature(cpf.RIVERS, zorder=104)
print(
'''\n
If the oceans are white, then the script is
working as designed, if not then not'''
)
plt.show()
print("Proof that the add_feature is actually working \n")
fig, ax = plt.subplots(1, 1, figsize=(12,9),
subplot_kw={'projection': ccrs.PlateCarree()})
ax.set_global()
ax.add_feature(cpf.OCEAN, facecolor="r")
ax.add_feature(cpf.COASTLINE)
ax.add_feature(cpf.BORDERS, linestyle='--')
ax.add_feature(cpf.LAKES, alpha=0.5)
ax.add_feature(cpf.RIVERS)
print(
'''If the oceans red then the ocean feature was added'''
)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment