Python中的 Matplotlib.pyplot.axhspan()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Pyplot是Matplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。
matplotlib.pyplot.axhspan()函数
matplotlib 库的 pyplot 模块中的axhspan()函数用于在轴上添加水平跨度(矩形)。
Syntax: matplotlib.pyplot.axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs)
Parameters: This method accept the following parameters that are described below:
- ymin: This parameter is the lower limit of the horizontal span in data units.
- ymax: This parameter is the upper limit of the horizontal span in data units.
- xmin: This parameter is the lower limit of the vertical span in data units.
- xmax: This parameter is the upper limit of the vertical span in data units.
Returns: This returns the Polygon.
下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.axhspan()函数:
示例 1:
import matplotlib.pyplot as plt
# xmin = 0 and xmax = 1 is the
# default value
plt.axhspan(0.25, 0.75, facecolor ='r', alpha = 0.7)
输出:
示例 2:
#Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(-2, 3, .01)
s = np.sin(np.pi * t)
plt.plot(t, s, color ='black')
plt.axhline(y = 1, color ='black')
plt.axvline(x = 1, color ='black')
plt.axvline(x = 0.5, ymin = 0.75, linewidth = 8,
color ='green')
plt.axhline(y =.5, xmin = 0.25, xmax = 0.75,
color ='black')
plt.axhspan(0.25, 0.75, facecolor ='0.5', alpha = 0.5)
plt.axvspan(2.25, 2.55, facecolor ='green', alpha = 0.5)
plt.title('matplotlib.pyplot.axhspan() Example\n',
fontsize=14, fontweight='bold')
plt.show()
输出: