Scatterplot trend line in Python matplotlib

Asked

Viewed 977 times

-1

Suppose I have two simple Python lists and create a scatter chart:

x = [548, 677, 987, 2, 29, 1114, 521, 999] 
y = [96, 775, 258, 369, 410, 99, 5, 1117]
import matplotlib.pyplot as plt
plt.scatter(x, y)

How can I create a trend line within that same graph?

1 answer

0

You can do this quickly through polyfit function of numpy.

import matplotlib.pyplot as plt
import numpy as np

x = [548, 677, 987, 2, 29, 1114, 521, 999] 
y = [96, 775, 258, 369, 410, 99, 5, 1117]

plt.scatter(x, y)

z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")

Exit:

inserir a descrição da imagem aqui

This one I made is the basics, but you can do some really cool things, as you can see here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.