This might help you The masking is definitely working correctly in the code from the question. You can visualize the ranges that get masked with fill_between. Also, sharing all axes makes it easier to compare the three plots.

code :

```
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["axes.xmargin"] = 0
plt.rcParams["axes.ymargin"] = 0
f1 = np.random.randint(51, size=150)
lt_vals = np.arange(0,25,1)
alt_vals = np.arange(0,15,1)
alt = np.tile(alt_vals,10)
lt = np.tile(lt_vals, 6)
x_vals = range(len(f1))
f1m = np.ma.masked_where((lt>5) & (lt<20), f1)
f1am = np.ma.masked_where(alt>5, f1m)
variables = [f1am, alt, lt]
ylabels = ['Function', 'Sim Alt', 'Sim Time']
number_of_subplots= len(variables)
fig, axes = plt.subplots(nrows=3, sharex=True)
for i,j,k in zip(range(number_of_subplots), variables, ylabels):
ax1 = axes[i]
ax1.plot(x_vals,j, marker=".")
ax1.set_ylabel(k)
axes[1].fill_between(x_vals,alt.max(),0, where=alt>5, alpha=0.2)
axes[2].fill_between(x_vals,lt.max(),0, where=(lt>5) & (lt<20), alpha=0.2)
axes[0].fill_between(x_vals,51,0, where=((lt>5) & (lt<20)) | (alt>5) , alpha=0.2)
plt.show()
```