How to plot data like points and draw line of linear fit in python?

Viewed 58

I have data in plotted on graph, how to draw linear fit line in graph?

I will be grateful if you can suggest solution, Thank you!

1 Answers

You can use inbuilt polyfit function to get linear fit value based on least square method for your data. But I am not aware how to maintain aspect ratio of inside grid 1:1.


angle = np.polyfit(x, y, 1)
y_line = angle[1] + angle[0] * x

fig, ax = plt.subplots(figsize=(8,8))
ax.scatter(x,y)
ax.plot(x,y_line, 'r')
Related