Python中的 Matplotlib.axes.Axes.axvline()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.axvline()函数
matplotlib 库的 axes 模块中的Axes.axvline()函数用于在轴上添加一条垂直线。
Syntax: Axes.axvline(self, x=0, ymin=0, ymax=1, **kwargs)
Parameters: This method accept the following parameters that are described below:
- x: This parameter is the x position in data coordinates of the vertical line with default value of 0.
- ymin: This parameter should be between 0 and 1, 0 being the bottom of the plot, 1 the top of the plot.Its default value of 0.
- ymax: This parameter should be between 0 and 1, 0 being the bottom of the plot, 1 the top of the plot. Its default value of 1.
Returns: This returns the following:
- lines:This returns the list of Line2D objects representing the plotted data.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.axhline()函数:
示例 1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.collections as collections
t = np.arange(0.0, 5, 0.01)
s1 = np.sin(4 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s1, color ='black', alpha = 0.75, lw = 1)
ax.axvline(3, color ='green', lw = 2, alpha = 0.75)
ax.set_title('matplotlib.axes.Axes.axvline() Example')
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
t = np.linspace(-10, 10, 100)
sig = 1 / t**2
fig, ax = plt.subplots()
plt.axvline(color ="green", alpha = 0.8, lw = 1.5)
plt.plot(t, sig, linewidth = 1.5, color ="black",
alpha = 0.6,
label = r"$\sigma(t) = \frac{1}{x ^ 2}$")
plt.xlim(-10, 10)
plt.xlabel("t")
plt.legend(fontsize = 14)
ax.set_title('matplotlib.axes.Axes.axvline() Example')
plt.show()
输出: