Matplotlib axvspan - 固体填充? [英] Matplotlib axvspan - solid fill?
问题描述
使用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')
不过,我希望它能通过图表来隐藏图表上的一行。目前它只是做到这一点。
我该如何填充这个axvspan实体和非透明?
它不是关于透明度(可以使用 alpha
参数)。这是关于 zorder
(一种到用户的距离,更低的zorder会更远)。您需要将 grid
的zorder放置在 axvspan
的zorder下(这又应该在下面,I想想情节
)。检查以下示例:
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()
,结果如下:
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:
这篇关于Matplotlib axvspan - 固体填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!