如果我有两条轮廓线
plt.contourf(xx, yy, zzmax, levels=[1], colors='r', alpha=0.8)
plt.contourf(xx, yy, zzmin, levels=[1], colors='r', alpha=0.8)
我如何绘制一个域来填充它们之间的区域?
(抱歉,如果这是个问题的话)
发布于 2022-02-03 08:32:39
下面的代码首先创建一些测试数据。蓝线表示zzmax
和zzmin
等于1
的位置。右边的子图用红色表示zzmax
小于1而zzmin
大于1的区域。
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np
from scipy.ndimage import gaussian_filter
xx = np.linspace(0, 10, 100)
yy = np.linspace(0, 8, 80)
np.random.seed(11235813)
zzmax = gaussian_filter(np.random.randn(len(yy), len(xx)) * 10 + 1, 8)
zzmin = gaussian_filter(np.random.randn(len(yy), len(xx)) * 10 + 0.9, 8)
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(15, 4))
cnt1 = ax1.contourf(xx, yy, zzmax, cmap='RdYlGn')
plt.colorbar(cnt1, ax=ax1)
ax1.contour(xx, yy, zzmax, levels=[1], colors='skyblue', linewidths=3)
ax1.set_title('zzmax')
cnt2 = ax2.contourf(xx, yy, zzmin, cmap='RdYlGn')
plt.colorbar(cnt2, ax=ax2)
ax2.contour(xx, yy, zzmin, levels=[1], colors='skyblue', linewidths=3)
ax2.set_title('zzmin')
ax3.contourf(xx, yy, (zzmax <= 1) & (zzmin >= 1), levels=[0.5, 2], cmap=ListedColormap(['red']), alpha=0.3)
ax3.set_title('zzmax ≤ 1 and zmin ≥ 1')
plt.tight_layout()
plt.show()
https://stackoverflow.com/questions/70970193
复制