'plotting white stuff over a plot in python

I've got some trouble with plotting white spans over a plot. I've created and plotted a given function y(x) and defined some "forbidden" regions, corresponding to some x intervals. Now I want to visually "erase" these forbidden portions by overplotting some white bars, by using the matplotlib axvspan. However, if I try to plot a white span, the underlying plot is still visible, although I use different alphas and although I put the axvspan command after the y(x) plot. Is there a way to make the white span cover the underlying plot? Thank you in advance.

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(18,8))
plt.step(np.arange(0,10,0.1), np.arange(0,10,0.1))
plt.axvspan(2,5, color='white')
plt.show()

enter image description here



Solution 1:[1]

Using axvspan will draw a portion "behind" your plot , as you can see here ([1]: https://i.stack.imgur.com/6dLWh.png) when i replaced white with red :

[import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(18,8))
plt.step(np.arange(0,10,0.1), np.arange(0,10,0.1))
plt.axvspan(2,5, color='red')
plt.show()][1]

You need to delete manually those portions for the array

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MK S2M