Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overplot a line on a scatter plot in python?

I have two vectors of data and I've put them into matplotlib.scatter(). Now I'd like to over plot a linear fit to these data. How would I do this? I've tried using scikitlearn and np.scatter.

like image 231
goldisfine Avatar asked Sep 28 '13 16:09

goldisfine


People also ask

How do you insert a horizontal line in Python?

In matplotlib, if you want to draw a horizontal line with full width simply use the axhline() method. You can also use the hlines() method to draw a full-width horizontal line but in this method, you have to set xmin and xmax to full width.


1 Answers

import numpy as np from numpy.polynomial.polynomial import polyfit import matplotlib.pyplot as plt  # Sample data x = np.arange(10) y = 5 * x + 10  # Fit with polyfit b, m = polyfit(x, y, 1)  plt.plot(x, y, '.') plt.plot(x, b + m * x, '-') plt.show() 

enter image description here

like image 102
Greg Whittier Avatar answered Sep 18 '22 20:09

Greg Whittier