python绘画库axvspan,Matplotlib axvspan - 固体填充?

本文介绍如何使用Matplotlib隐藏axvspan线并创建实心填充区域。关键在于理解zorder概念,通过设置grid的zorder低于axvspan来实现。示例代码演示了如何将网格的层级置于axvspan之下,从而达到预期效果。
摘要由CSDN通过智能技术生成

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.

6DFpg.png

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:

V0FiW.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值