axhline函数使用–Matplotlib
该函数用法可平移至axvline上
函数功能: Add a horizontal line across the axis.
在轴上添加一条水平线
函数语法: axhline(y=0, xmin=0, xmax=1, **kwargs)
函数参数:
y: float, default: 0 ;y position in data coordinates of the horizontal line.
浮点型,默认值0,水平线在坐标中y点的位置
xmin: float, default: 0 ;Should be between 0 and 1, 0 being the far left of the plot, 1 the far right of the plot.
浮点型,默认值为0,取值位于[0,1]之间,取值0时在图的左边界,取值1时在图的右边界。
xmax: float, default: 1;Should be between 0 and 1, 0 being the far left of the plot, 1 the far right of the plot.
浮点型,默认值为1,取值位于[0,1]之间,0取值时在图的左边界,取值1时在图的右边界。
上面两个参数xmin,xmax指定水平线的起始位置与结束位置
**kwargs: 其他关键参数,如:水平线的颜色、风格、粗细等
无参数传入:默认在y=0的位置,从最左侧(0)到最右侧(1),实线绘制
绘制y = 0.6
则xmin取值为[0,1],绘制范围为边右边界值*xmin,如本图中:xmin=0.2,xmax=0.8表示绘制水平线的范围为
10
∗
0.2
10*0.2
10∗0.2~
10
∗
0.8
10*0.8
10∗0.8之间
其他参数:如设置水平线的粗细,风格,颜色
axvline函数
函数功能: Add a vertical line across the axes.
在轴上添加一条垂直线
函数语法: axhline(x=0, xmin=0, ymax=1, **kwargs)
函数参数:
x: float, default: 0 ;x position in data coordinates of the vertical line.
浮点型,默认值0,垂直线在坐标中x点的位置
ymin: float, default: 0 ;Should be between 0 and 1, 0 being the bottom of the plot, 1 the top of the plot.
浮点型,默认值为0,取值位于[0,1]之间,取值0时在图的下边界,取值1时在图的上边界。
ymax: float, default: 1;Should be between 0 and 1, 0 being the bottom of the plot, 1 the top of the plot.
浮点型,默认值为1,取值位于[0,1]之间,取值0时在图的下边界,取值1时在图的上边界。
上面两个参数ymin,ymax指定垂直线的起始位置与结束位置
**kwargs: 其他关键参数,如:水平线的颜色、风格、粗细等
具体相关信息可参看上面的axhline
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.grid() #绘制网格线
plt.xlim(0,10) #调整x轴边界
plt.axhline(y = 0.6,xmin = 0.2,xmax = 0.8, c = 'r',ls = '--',lw = 1) #绘制平行于x轴的水平线
plt.axvline(x = 5,ymin = 0.2,ymax = 0.8, c = 'g',ls = '-',lw = 3) #绘制平行于y轴的垂直线
plt.show()