0

I am trying to do something fairly simple and can't find a simple way to do it. I'm trying to shade a region of a plot so that it is completely covered. I have an example code that does almost what I want below, but you can still see the data in green. I want to cover that completely.

import matplotlib.pyplot as plt
import numpy as np

plt.plot(np.linspace(0,2.0 * np.pi,30),np.sin(np.linspace(0,2.0 * np.pi,30)),color='green')
plt.axhspan(-1.5, 1.5, xmin=0.66, xmax=1,color='gray')
plt.xlim(0,2.0 * np.pi)
plt.ylim(-1.5,1.5)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Joe
  • 19
  • 3

1 Answers1

1

Use the zorder parameter to change which elements are drawn on top:

import matplotlib.pyplot as plt
import numpy as np

plt.plot(np.linspace(0,2.0 * np.pi,30),np.sin(np.linspace(0,2.0 * np.pi,30)),color='green', zorder = 0)
plt.axhspan(-1.5, 1.5, xmin=0.66, xmax=1,color='gray', zorder = 1)
plt.xlim(0,2.0 * np.pi)
plt.ylim(-1.5,1.5)
Michael Cao
  • 2,278
  • 1
  • 1
  • 13