Pages

Friday, July 20, 2018

Plotting Data Contained in a Text File: Python

Using a plain-text editor, create a text document that contains the following data...


Save the text file to the same folder you save your python program file. I've called my text document 'dataPoints.txt'.

The following Python code will show you how to open the file and extract the data so that you can use it to plot a graph, and if you like, perform a regression analysis...


Notice the values 'x2' and 'myYVals' used to calculate the best fit curve. Had we just gone with 'xVals' and 'calcYVals' in the 'Quadratic Fit' plot, the result would have been this...

Not very smooth. But, if we plot according to the code above, we get...

Which looks much better.

Don't forget to include the 'rSquare' function, which was included in my last post.

No comments:

Post a Comment