cartopy
cartopy copied to clipboard
Usage of non-uniform levels in contourf not getting clipped?
Description
There seems to be a bug in contourf or maybe this is expected behavior. Nonuniform levels seem to "undo" masking or nan masked arrays.
Code to reproduce
data
: data.npz.zip
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import cartopy
from cartopy.crs import PlateCarree, Mollweide
import numpy as np
data = np.load("data.npz")
lat = data['lat']
lon = data['lon']
depth = data['depth']
# Get cmap
colormap = plt.get_cmap('jet')
# Create colormap and define uniform levels
levels_unif = np.arange(0.0, 700.0, 50.0)
colors = colormap(np.linspace(0, 1, len(levels_unif)))
cmap = ListedColormap(colors)
# Create colormap and define non uniform levels
levels_nonunif = np.array([0.0, 10.0, 15.0, 25.0, 75.0, 100.0, 150.0, 200.0,
300.0, 400.0, 600.0])
colors = colormap(np.linspace(0, 1, len(levels_nonunif)))
cmap = ListedColormap(colors)
plt.figure(figsize=(10, 5))
ax = plt.subplot(121, projection=Mollweide(central_longitude=-150.0))
ax.add_feature(cartopy.feature.LAND, edgecolor='black',
linewidth=0.5, facecolor=(0.9, 0.9, 0.9))
plt.contourf(lon, lat, depth, levels=levels_unif,
transform=PlateCarree(central_longitude=0.0),
cmap=cmap)
plt.colorbar(orientation='horizontal', aspect=50)
ax2 = plt.subplot(122, projection=Mollweide(central_longitude=-150.0))
ax2.add_feature(cartopy.feature.LAND, edgecolor='black',
linewidth=0.5, facecolor=(0.9, 0.9, 0.9))
plt.contourf(lon, lat, depth, levels=levels_nonunif,
transform=PlateCarree(central_longitude=0.0),
cmap=cmap)
plt.colorbar(orientation='horizontal', aspect=50)