axvspan函数使用–Matplotlib
函数功能、函数语法与参数说明可平移至axhspan
函数功能: Add a vertical span (rectangle) across the axes.
在坐标系添加垂直区域(矩形)
The rectangle spans from xmin to xmax horizontally, and, by default, the whole y-axis vertically. The y-span can be set using ymin (default: 0) and ymax (default: 1) which are in axis units; e.g. ymin = 0.5 always refers to the middle of the y-axis regardless of the limits set by set_ylim.
矩形区域水平上范围从xmin到xmax,默认整个垂直y轴区域。y区域调整可以通过设置ymin(默认0) 与ymax(默认1)实现。ymin=0.5总是在y轴的中间。
函数语法: axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs)
函数参数:
xmin: float;Lower x-coordinate of the span, in data units
浮点型,水平区域的左侧边界,以数据单位表示
xmax: float Upper x-coordinate of the span, in data units.
浮点型,水平区域的右侧边界,以数据单位表示
ymin: float, default: 0; Lower y-coordinate of the span, in y-axis units (0-1).
浮点型,默认值为0,取值[0,1] ; 区域y轴的下边界,以坐标单位表示
ymax: float, default: 1 Upper y-coordinate of the span, in y-axis units (0-1).
浮点型,默认值为1,取值[0,1] ; 区域y轴的上边界,以坐标单位表示
ymin,ymax是以比例表示的,取值在[0,1]之间。
**kwargs: Polygon properties,属性具体包含如下:
在axvspan函数中,xmin,xmax是必输参数,无默认值,不写会报错
传入xmin,xmax设置区域的水平方向上的起始值与终止值,可以看到区域在水平方向上的4-6范围内,而竖直的y轴方向上则贯穿全部。
设置y轴方向的最大、最小值,修改默认整个y轴的区域设置,指定在y轴的区间,ymin=0.25,ymax=0.5,则y轴方向下边界为:y轴方向最小值+y轴区间长度* ymin,即
−
1
+
(
1
−
(
−
1
)
)
∗
0.25
=
−
0.5
-1+(1-(-1))*0.25=-0.5
−1+(1−(−1))∗0.25=−0.5,同理可得y轴方向上边界值。
修改参考与区域的颜色透明度
颜色通过color参数指定,
透明度通过alpha参数指定,alpha取值位于[0,1]之间,取值为0,区域完全透明,不可见;取(0,1]之间的值即可。
当alpha=0
当alpha=1
当alpha位于(0,1)之间,alpha = 0.3
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y,ls = '-.',lw = 2,c='c',label = 'plot figure')
plt.legend(loc = 'lower left') # 设定图例位置
plt.ylim(-1,1) #修改y轴边界
plt.grid() #设置网格线
plt.axvspan(xmin = 4,xmax= 6,ymin = 0.25,ymax = 0.5, color = 'yellow',alpha = 0.3) #绘制垂直于x轴的参考区域
plt.show()
axhspan函数
函数功能: Add a horizontal span (rectangle) across the axis.
在坐标系添加水平区域(矩形)
The rectangle spans from ymin to ymax vertically, and, by default, the whole x-axis horizontally. The x-span can be set using xmin (default: 0) and xmax (default: 1) which are in axis units; e.g. xmin = 0.5 always refers to the middle of the x-axis regardless of the limits set by set_xlim.
矩形区域垂直上范围从ymin到ymax,默认整个水平x轴区域。x区域调整可以通过设置xmin(默认0) 与xmax(默认1)实现。xmin=0.5总是在x轴的中间。
函数语法: axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs)
函数参数:
ymin: float Lower y-coordinate of the span, in data units.
浮点型,垂直区域的下边界,以数据单位表示
ymax: float Upper y-coordinate of the span, in data units.
浮点型,垂直区域的上边界,以数据单位表示
xmin: float, default: 0 Lower x-coordinate of the span, in x-axis (0-1) units.
浮点型,默认值为0,取值[0,1] ; 区域x轴的左边界,以坐标单位表示
xmax: float, default: 1 Upper x-coordinate of the span, in x-axis (0-1) units.
浮点型,默认值为1,取值[0,1] ; 区域x轴的右边界,以坐标单位表示
**kwargs: Polygon properties,具体可修改属性同上。
ymin,ymax是必输参数,无默认值。
xmin,xmax是以比例表示的,取值在[0,1]之间。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y,ls = '-.',lw = 2,c='c',label = 'plot figure')
plt.legend(loc = 'lower left') # 设定图例位置
plt.xlim(0,10) #修改x轴边界
plt.grid() #设置网格线
plt.axhspan(ymin = -0.25,ymax= 0.25,xmin =0.75,xmax = 1, color = 'red',alpha = 0.6) #绘制垂直于y轴的参考区域
plt.show()