Pages

Friday, July 20, 2018

Python Plot With Data Set

Consider the following data set...

x
y
80.5
44
70.5
35
57
20.4
66
33
68
31
72
35
52
18.5
73.5
37
53
26


In python, we could make two lists, one for x-values and one for y-values.
xVals = [80.5, 70.5, 57, 66, 68, 72, 52, 73.5, 53]
yVals = [44, 35, 20.4, 33, 31, 35, 18.5, 37, 26]

This time, we're going with the standard Spyder Python editor, and not entering code directly into the iPython console. We've defined a function that handles our plotting instructions. The code should be pretty easy to follow. This time, we didn't set the range for the x-values; Pylab handled this for us. Also, in pylab.plot, we have 'bo' instead of just 'b'; this plots blue circles for the data points instead of a blue curve based on them. We've also added a legend, though we haven't made much use of it yet. loc = 'best' allows pylab to choose the best location for the legend so it doesn't get in the way of the important stuff.

Call the function and ask pylab to show the graph


















Our data
This data is begging for a best-fit curve, and we guess it's a line. So, we have to modify our function a little to get that. To get an idea of how good our fit is, we need to calculate an $R^{2}$ value. We define another function to do that, and to make use of that function, we define xVals and yVals as pylab arrays. We also take advantage of  polyfit and  polyval to calculate a best fit line to plot. In this case, we consider the first-degree polynomial that is the equation of a line, y = mx + b, where m is the slope of the line and b is the vertical intercept. Those coefficients are calculated in polyfit and are stored in 'model', which gets used in polyval to calculate the y-values for our model line.



An $R^{2}$ value of around 0.90 isn't too bad.

Next time, we'll use a data set contained in a text file.



No comments:

Post a Comment