How to add trendline in python matplotlib dot (scatter) graphs?

Question:

How could I add a trendline to a dot graph drawn using matplotlib.scatter?

Asked By: user3476791

||

Answers:

as explained here

With help from numpy one can calculate for example a linear fitting.

# plot the data itself
pylab.plot(x,y,'o')

# calc the trendline
z = numpy.polyfit(x, y, 1)
p = numpy.poly1d(z)
pylab.plot(x,p(x),"r--")
# the line equation:
print "y=%.6fx+(%.6f)"%(z[0],z[1])
Answered By: martinenzinger

Trendline for a scatter plot is the simple regression line. The seaborn library has a function (regplot) that does it in one function call. You can even draw the confidence intervals (with ci=; I turned it off in the plot below).

import seaborn as sns
sns.regplot(x=x_data, y=y_data, ci=False, line_kws={'color':'red'});

The above call produces the following plot for the following dataset:

import numpy as np
x_data, y_data = np.repeat(np.linspace(0, 9, 100)[None,:], 2, axis=0) + np.random.rand(2, 100)*2

res

If you were using subplots, you can pass the ax= as well.

import matplotlib.pyplot as plt
fig, axs = plt.subplots(1,2, figsize=(12,3))
axs[0].scatter(x_data, y_data)
sns.regplot(x=x_data, y=y_data, ci=False, line_kws={'color':'red'}, ax=axs[1]);

res2


Simple regression coefficients have a closed form solution so you can also solve explicitly for them and plot the regression line along with the scatter plot.

If x_data and y_data are lists:

x_mean = sum(x_data) / len(x_data)
y_mean = sum(y_data) / len(y_data)
covar = sum((xi - x_mean) * (yi - y_mean) for xi, yi in zip(x_data, y_data))
x_var = sum((xi - x_mean)**2 for xi in x_data)
beta = covar / x_var
alpha = y_mean - beta * x_mean
y_hat = [alpha + beta * xi for xi in x_data]

If x_data and y_data are numpy arrays:

x_mean, y_mean = np.mean(x_data), np.mean(y_data)
beta = np.sum((x_data - x_mean) * (y_data - y_mean)) / np.sum((x_data - x_mean)**2)
alpha = y_mean - beta * x_mean
y_hat = alpha + beta * x_data

Then just draw the two plots:

import matplotlib.pyplot as plt
plt.plot(x_data, y_data, 'bo', x_data, y_hat, "r-");
Answered By: cottontail
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.