Matplotlib.pyplot.axhspan() in Python
Last Updated :
27 Apr, 2020
Improve
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.
The axhspan() function in pyplot module of matplotlib library is used to add a horizontal span (rectangle) across the axis.
Python3 1==
Output:
Example 2:
Python3
Output:
matplotlib.pyplot.axhspan() Function
Syntax: matplotlib.pyplot.axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs) Parameters: This method accept the following parameters that are described below:Below examples illustrate the matplotlib.pyplot.axhspan() function in matplotlib.pyplot: Example 1:Returns: This returns the Polygon.
- 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.
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)

#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()
