Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a trend line to a matplotlib line plot python

Apologies if this has already been asked but I can't find the answer anywhere. I want to add an overall trend line to a plt plot. Sample data:

import pandas as pd
data = pd.DataFrame({'year': [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 
                              2019],
                     'value': [2, 5, 8, 4, 1, 6, 10, 14, 8]})

import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [28, 26]
data.plot(x = "year", y = "value", fontsize = 30)
plt.xlabel('Time', fontsize = 30)

enter image description here

How can I add a trend line?

like image 746
prmlmu Avatar asked Jan 22 '19 12:01

prmlmu


People also ask

How do you add a trendline in Python?

Add a Trendline With NumPy in Python Matplotlib We calculate the trendline with NumPy . To do that, we need the x- and y-axis. Then we use the polyfit and poly1d functions of NumPy . And finally, we plot the trendline.

What NumPy function do you use to add a trend line to a scatter plot?

To create a polynomial trendline, simply change the value in the np. polyfit() function.


1 Answers

If you are looking for a simple linear regression fit, you can use directly either lmplot or regplot from seaborn. It performs the linear regression and plots the fit (line) with a 95% confidence interval (shades, default value). You can also use NumPy to perform the fit. In case you want to use NumPy, comment below and I will update.

import seaborn as sns

# Your DataFrame here 

# sns.lmplot(x='year',y='value',data=data,fit_reg=True) 

sns.regplot(x='year',y='value',data=data, fit_reg=True) 

enter image description here

From the Docs

The regplot() and lmplot() functions are closely related, but the former is an axes-level function while the latter is a figure-level function that combines regplot() and FacetGrid which allows you to plot conditional relationships amongst your data on different subplots in the grid.

like image 75
Sheldore Avatar answered Oct 21 '22 18:10

Sheldore