Python中的 Matplotlib.pyplot.hlines()
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。
Matplotlib.pyplot.hlines()
Matplotlib.pyplot.hlines()
用于在图形中从 xmin 到 xmax 的每个 y 处绘制水平线。
Syntax: matplotlib.pyplot.hlines(y, xmin, xmax, colors=’k’, linestyles=’solid’, label=”, *, data=None, **kwargs
Parameters:
The Matplotlib.pyplot.hlines()
accepts the below-described parameters:
- y : It is a required parameter for this method. This parameter describes that in the graph the line is to be drawn. Its value is a scalar or sequence of scalars, in other words, it is the y-indexes where the line is to be plotted.
- xmin: It is a required parameter that has either a scalar value or a 1D array-like value that sets the beginning of each line. If scalars are provided all the lines will have the same length.
- colors: As the name suggests it is used to set the color of the line to be plotted. This parameter is optional in nature and its default value is ‘k’
- linestyles: It is also an optional parameter that accepts four values namely ‘solid’, ‘dashed’, ‘dashdot’ and ‘dotted’. It is responsible for setting the style of the line to be plotted.
- label: It is an optional parameter used to describe information about the plotted line in the same line. This accepts a string whose default value is an empty string.
- **kwargs: This parameter is used to make use of LineCollection properties in the plotted line.
注意:除上述参数外,该方法还可以带一个数据关键字参数。还需要注意的是,作为数据传递的对象必须支持项目访问和成员资格测试。
示例 1:
from matplotlib import pyplot as plt
plt.hlines(y = 1, xmin = 1, xmax = 4)
plt.hlines(y = 1.6, xmin = 1.5, xmax = 4.5)
plt.hlines(y = 2, xmin = 2, xmax = 5)
输出 :
示例 2:
from matplotlib import pyplot as plt
plt.hlines(y = 1, xmin = 1, xmax = 4, label ="black line")
plt.hlines(y = 1.6, xmin = 1.5, xmax = 4.5, color ='r')
plt.text(1, 1.6, 'Red line', ha ='left', va ='center')
plt.hlines(y = 2, xmin = 2, xmax = 5)
输出 :