-1

I am writing a code to find the least squares of best fit line of some data in an imported file. The equation of the line is ax+b where I have calculated a and b already. To plot the line I have tried:

LS_fit_ydata = []
for i in x_data:
    y_new = ((i*b) + a)
    LS_fit_ydata.append(y_new)

I am using matplotlib.pyplot as plt to plot my graph.

There are no error messages but the line does not appear on my graph. Does anyone know what's going wrong? Thank you for any help you can provide.

linusg
  • 6,289
  • 4
  • 28
  • 78
  • 1
    So far, the code you shared has no plotting in it. It's merely going through a for-loop, and appending y_new to a list – Adib May 04 '16 at 12:13

1 Answers1

0

What you're missing is the plotting part in the code:

# The code you provided
LS_fit_ydata = []
for i in x_data:
    y_new = ((i*b) + a)
    LS_fit_ydata.append(y_new)

# What happens here is you're plotting x against y one by one via the list
plt.plot(x_data, y_new)
# Show the graph
plt.show()
Adib
  • 1,282
  • 1
  • 16
  • 32
  • Thank you! Do you know how I can make y_new into a numpy array? I keep getting value error x and y not same dimensions – Jess Gates May 04 '16 at 19:37
  • @JessGates The documentations here provide ample amount of examples and information: http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.array.html – Adib May 04 '16 at 21:15