Pages

Showing posts with label pylab. Show all posts
Showing posts with label pylab. Show all posts

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.

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.



Simple Graph Plot in Python


First, let's explore basic function graphing using the iPython console. (I'm using Spyder's iPython console.)

We'll consider an elementary power function, namely the quadratic function $ f(x) =  x^{2} $

Steps:
  1. import pylab and numpy
  2. set the range of x-values. Here, we'll go from -5 to  +5, with a step of 0.01
  3. define the function y in terms of xVals (x-values)
  4. label the x and y axes
  5. give the graph a title. Here, we keep it simple with f(x) = x^2
  6. plot the graph. Here, xVals are the x-values, y are the y-values, and 'b' makes the curve blue
Enter the code directly into the iPython console
  
Our graph, nice and simple
If you don't want the graph to appear inline (inside the iPython console), in Spyder you can go to...
Tools >> Preferences >> iPython Console >> Graphics >> Graphics Backend, and set the backend to Automatic.

Next time, we'll plot some data points.