-1

I'm making a very basic plot. I have a csv data set that looks like this:

1,280.6
2,280.2
3,276.6
4,279.6
5,277.4
6,279.4
7,274.2
8,278.2
9,276.4
10,279.4
11,274.6
12,276.2
13,274.4
14,277.8

and I am plotting it with matplotlib like this:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('dataset.csv', delimiter=',',header=None,names=['x','y'])

plt.plot(df['x'], df['y'], label='',color=current_palette)

plt.xlabel('x')
plt.ylabel('y')
plt.title('Title')
plt.show()

which gives this: a pretty graph

From both my knowledge and from previous answers I've found on here, I know how to calculate line of best fit when I am plotting a given equation or a range or similar. But what would be the best way to find a line of best fit for a given set of data?

Thanks a lot!

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
albc
  • 23
  • 2
  • 6
  • 1
    This question is too general and needs a lot of information you haven't provided. Curve fitting method really depends on the nature of your data and how the results will be used. – anishtain4 Jun 07 '18 at 23:43
  • I'm quite new to this -- could you tell me what kind of information is further needed? I am also just wondering which method would be best so I'm not looking for advice on how to do anything in particular, just examples of how I *could* do it? This isn't for anything serious either so I won't be using the results for anything important. Sorry if I'm not answering this well. – albc Jun 07 '18 at 23:58
  • Possible duplicate of [Linear regression with matplotlib / numpy](https://stackoverflow.com/questions/6148207/linear-regression-with-matplotlib-numpy) – Diziet Asahi Jun 08 '18 at 07:23

1 Answers1

0

For finding the line of best fit, I would recommend using scipy's linear regression module.

from scipy.stats import linregress
slope, intercept, r_value, p_value, std_err = linregress(df['x'], df['y'])

Now that you have the slope and intercept, you can plot the line of best fit.

Halee
  • 492
  • 9
  • 15
Vivek
  • 507
  • 5
  • 15