我想向 Xarray DataArray 数据添加点画以指示重要性。该数据是经纬度网格上的二维气候数据。我想提供一个 True/False 掩码来绘制映射的变量数据。我正在尝试使用轮廓来达到此目的,但如果其他方法更合适,我愿意接受其他方法。
我尝试使用“轮廓阴影线”来点画重要区域,但点画点覆盖了所有绘图区域。当我使用遮罩和影线添加 contourf
时,0 和 1 值也以纯色绘制,遮盖了我想要可视化的变量数据。
和R的类似问题“向图像轮廓图添加点画”。
这个问题可能类似。正如该问题的答案所建议的,我尝试提供 levels
但这并没有解决我的问题。
所需的输出将是左轮廓图,仅在掩码为 True 的情况下带有点画点。
右图显示了我的尝试。您可以看到,无论掩码如何,点画影线都覆盖在所有绘图上,并且掩码值会模糊 DataArray 的值。
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
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=["."])
ax2.contourf(da.lon, da.lat, mask, hatches=['', '.'], alpha=0)
这种行为只在我最新版本的 Matplotlib 中才开始发生。