- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to draw an average line for a scatter plot in MatPlotLib?
To draw an average line for a plot in matplotlib, we can take the following steps −
Set the figure size and adjust the padding between and around the subplots.
Make x and y data points using numpy.
Use subplots() method to create a figure and a set of subplots.
Use plot() method for x and y data points.
Find the average value of the array, x.
Plot x and y_avg data points using plot() method.
Place a legend on the figure.
To display the figure, use show() method.
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([3, 4, 5, 6, 7, 8, 9]) y = np.array([6, 5, 4, 3, 2, 1, 6]) fig, ax = plt.subplots() ax.plot(x, y, 'o-', label='line plot') y_avg = [np.mean(x)] * len(x) ax.plot(x, y_avg, color='red', lw=6, ls='--', label="average plot") plt.legend(loc=0) plt.show()
- How can I draw a scatter trend line using Matplotlib?
- How to animate a scatter plot in Matplotlib?
- How to make a discrete colorbar for a scatter plot in matplotlib?
- Adding a line to a scatter plot using Python's Matplotlib
- Python - Draw a Scatter Plot for a Pandas DataFrame
- How to plot scatter masked points and add a line demarking masked regions in Matplotlib?
- How to use different markers for different points in a Pylab scatter plot(Matplotlib)?
- How to overplot a line on a scatter plot in Python?
- Plot scatter points using plot method in Matplotlib
- How to plot additional points on the top of a scatter plot in Matplotlib?
- How to draw a line outside of an axis in Matplotlib?
- Adding caption below X-axis for a scatter plot using Matplotlib
- How to animate a line plot in Matplotlib?
- How to make a scatter plot for clustering in Python?
- How to plot scatter points in a 3D figure with a colorbar in Matplotlib?