When you set an n-degree polynomial p(x) = a0 + a1*x + a2*x**2 + ... + an*x**n into a set of data points (x0, y0), (x1, y1), ..., (xm, y_m) , the np.lstsq call is made using the coefficient matrix, which looks like this:
[1 x0 x0**2 ... x0**n] [1 x1 x1**2 ... x1**n] ... [1 xm xm**2 ... xm**n]
If you remove the j column from this matrix, you effectively set this coefficient in the polynomial to 0. Thus, to get rid of the coefficient a0 , you can do the following:
def fit_poly_through_origin(x, y, n=1): a = x[:, np.newaxis] ** np.arange(1, n+1) coeff = np.linalg.lstsq(a, y)[0] return np.concatenate(([0], coeff)) n = 1000 x = np.random.rand(n) y = 1 + 3*x - 4*x**2 + np.random.rand(n)*0.25 c0 = np.polynomial.polynomial.polyfit(x, y, 2) c1 = fit_poly_through_origin(x, y, 2) p0 = np.polynomial.Polynomial(c0) p1 = np.polynomial.Polynomial(c1) plt.plot(x, y, 'kx') xx = np.linspace(0, 1, 1000) plt.plot(xx, p0(xx), 'r-', ) plt.plot(xx, p1(xx), 'b-', )

Jaime source share