Home > Enterprise >  Is there a function in python to fill the area between two contourlines each given by different func
Is there a function in python to fill the area between two contourlines each given by different func

Time:02-05

If I have two contourlines given by

plt.contourf(xx, yy, zzmax, levels=[1], colors='r', alpha=0.8)
plt.contourf(xx, yy, zzmin, levels=[1], colors='r', alpha=0.8)

how do I plot a domain which fills the area between them?

(Sorry if this is a noob question)

CodePudding user response:

The following code first creates some test data. The blue lines indicate where zzmax and zzmin are equal to 1. The subplot at the right shows in red the region where both zzmax is smaller than 1 and zzmin is larger than 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()

coloring between contours

  •  Tags:  
  • Related