I want to add stippling to Xarray DataArray data to indicate significance. The data is 2D climate data on a lat lon grid. I want to give a True/False mask to plot over mapped variable data. I am trying to use contourf for this purpose, but I am open to other methods if they are more suitable.
I have tried using contourf hatching to stipple the significant regions, but the stipple dots cover all the plot area.
I have found MATLAB stipple documentation and a similar question for R “adding stippling to image contour plot”.
Below is an illustrative example of my problem.
The desired output would be the left contourf plot with stippling dots only where the mask is True.
The right plot shows my attempt. You can see that the stippling hatches are over all the plot, regardless of the mask and the mask values are obscuring the values of the DataArray.
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
def f(x, y):
np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
return
x = np.linspace(0, 5, 51)
y = np.linspace(0, 5, 41)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
da = xr.DataArray(Z, coords = {"lon":x, "lat":y}, dims=["lat", "lon"])
fig =plt.figure(figsize=(12, 6))
ax1 = plt.subplot(121)
ax2 = plt.subplot(122)
ax1.contourf(da.lon, da.lat, da)
mask = da>0.5
ax2.contourf(da.lon, da.lat, da)
ax2.contourf(da.lon, da.lat, mask, hatches=["."])
IMAGE | left: contourf plot | right: mask with stippling
Gen Tolhurst is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.