matplotlib.pyplot.axvline()函数
Matplotlib是一个在Python中创建静态、动画和交互式可视化的绘图库。Matplotlib可以用于Python脚本、Python和IPythonshell、web应用服务器,以及各种图形用户界面工具包,如Tkinter、awxPython等。
Pyplot是一个Matplotlib模块,它提供了一个类似matlab的接口。Matplotlib被设计成与MATLAB一样好用,具有使用Python的能力以及免费和开源的优势。
matplotlib.pyplot.axvline()
这个函数在图形的轴线上添加竖线
Syntax:
matplotlib.pyplot.axvline(x=0, ymin=0, ymax=1, **kwargs)
参数:
X:放置垂直线在数据坐标中的X位置
Ymin:竖线在y轴上的起始位置,它将取0到1之间的值,0是轴的底部,1是轴的顶部
Ymax:竖线在y轴上的结束位置,它将取0到1之间的值,0是轴的底部,1是轴的顶部
**kwargs:用于更改行属性的其他可选参数,例如
改变颜色,线宽等
示例1
# Importing matplotlib.pyplot as plt
import matplotlib.pyplot as plt
# Initialising values of x and y
x =[0, 5, 10, 15, 20]
y =[1, 3, 5, 6, 9]
# Plotting the graph
plt.plot(x, y)
# Drawing red vertical line at
# x = 2.5 starting at half the
#length of y axis(ymin = 0.5) and
#continuing till the end(ymax = 1)
# And setting the color of line to red
plt.axvline(x = 2.5, ymin = 0.5, ymax = 1,
color ='red')
plt.show()
输出:
示例2
import matplotlib.pyplot as plt
x =[0, 5, 10, 15, 20]
y =[1, 3, 5, 6, 9]
plt.plot(x, y)
# Drawing vertical line from 25 %
# of the y-axis length to 80 %
# And also increasing the linewidth
plt.axvline(x = 2.5, ymin = 0.25, ymax = 0.80,
linewidth = 8, color ='green')
plt.show()
输出:
示例3
import matplotlib.pyplot as plt
x =[0, 5, 10, 15, 20]
y =[1, 3, 5, 6, 9]
plt.plot(x, y)
# Drawing vertical line from 25 %
# of the y-axis length to 75 %
# And also changing the linestyle
plt.axvline(x = 2.5, ymin = 0.25, ymax = 0.75,
linewidth = 4, linestyle ="--",
color ='red')
plt.show()
输出: