axhline函数--Matplotlib

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 100.2~ 10 ∗ 0.8 10*0.8 100.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()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值