I'm using this line of code to create a vertical span across a graph using matplotlib.
matplotlib.pyplot.axvspan(datetime.datetime.strptime("09-10-2015", '%d-%m-%Y'), datetime.datetime.strptime(now.strftime("%d-%m-%Y"), '%d-%m-%Y'), fill=True, linewidth=0, color='r')
However I'd like it to go over the graph to hide the line on the graph. At the moment it just does this.
How can I make the fill of this axvspan solid and non transparent?
解决方案
It's not about transparency (which you can change with the alpha parameter). It's about zorder (a kind of distance to the user, lower zorder will be farther). You need to put the zorder of the grid below the zorder of the axvspan (which in turn should be below, I think, of the plot). Check the following example:
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(-1, 2, .01)
s = np.sin(2*np.pi*t)
plt.plot(t, s,zorder=4)
p = plt.axvspan(1.25, 1.55, facecolor='g', alpha=1,zorder=3)
plt.axis([-1, 2, -1, 2])
plt.grid(zorder=2)
plt.show()
, which results in: