Matplotlib.pyplot.hlines() in Python (original) (raw)

Last Updated : 19 Apr, 2020

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.

Matplotlib.pyplot.hlines()

The Matplotlib.pyplot.hlines() is used to draw horizontal lines in a graph at each y from xmin to xmax.

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:

Note: In addition to the above-mentioned parameters, this method can take a data keyword argument. It is also important to note that the object passed as data must support item access and membership test.

Example 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 )

Output :
Matplotlib.pyplot.hlines()

Example 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 )

Output :
Matplotlib.pyplot.hlines()