Session2

34
Python for Science and Engg: Plotting experimental data FOSSEE Department of Aerospace Engineering IIT Bombay 12 February, 2010 Day 1, Session 2 FOSSEE (IIT Bombay) Plotting with Python 1 / 34

Transcript of Session2

Page 1: Session2

Python for Science and Engg:Plotting experimental data

FOSSEE

Department of Aerospace EngineeringIIT Bombay

12 February, 2010Day 1, Session 2

FOSSEE (IIT Bombay) Plotting with Python 1 / 34

Page 2: Session2

Outline

1 Plotting Points

2 Lists

3 Simple Pendulum

4 Strings

5 Summary

FOSSEE (IIT Bombay) Plotting with Python 2 / 34

Page 3: Session2

Plotting Points

Outline

1 Plotting Points

2 Lists

3 Simple Pendulum

4 Strings

5 Summary

FOSSEE (IIT Bombay) Plotting with Python 3 / 34

Page 4: Session2

Plotting Points

Why would I plot f(x)?

Do we plot analytical functions or experimental data?In []: x = [0, 1, 2, 3]

In []: y = [7, 11, 15, 19]

In []: plot(x, y)Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>]

In []: xlabel(’X’)Out[]: <matplotlib.text.Text object at 0x986e9ac>

In []: ylabel(’Y’)Out[]: <matplotlib.text.Text object at 0x98746ec>

FOSSEE (IIT Bombay) Plotting with Python 4 / 34

Page 5: Session2

Plotting Points

Is this what you have?FOSSEE (IIT Bombay) Plotting with Python 5 / 34

Page 6: Session2

Plotting Points

Plotting points

What if we want to plot the points!

In []: clf()

In []: plot(x, y, ’o’)Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]

In []: clf()In []: plot(x, y, ’.’)Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]

FOSSEE (IIT Bombay) Plotting with Python 6 / 34

Page 7: Session2

Plotting Points

FOSSEE (IIT Bombay) Plotting with Python 7 / 34

Page 8: Session2

Plotting Points

Additional Plotting Attributes

’o’ - Filled circles’.’ - Small Dots’-’ - Lines’- -’ - Dashed lines

FOSSEE (IIT Bombay) Plotting with Python 8 / 34

Page 9: Session2

Lists

Outline

1 Plotting Points

2 Lists

3 Simple Pendulum

4 Strings

5 Summary

FOSSEE (IIT Bombay) Plotting with Python 9 / 34

Page 10: Session2

Lists

Lists: Introduction

In []: x = [0, 1, 2, 3]

In []: y = [7, 11, 15, 19]

What are x and y?

lists!!

FOSSEE (IIT Bombay) Plotting with Python 10 / 34

Page 11: Session2

Lists

Lists: Initializing & accessing elements

In []: mtlist = []

Empty List

In []: p = [ 2, 3, 5, 7]

In []: p[0]+p[1]+p[-1]Out[]: 12

FOSSEE (IIT Bombay) Plotting with Python 11 / 34

Page 12: Session2

Lists

List: Slicing

Remember. . .In []: p = [ 2, 3, 5, 7]

In []: p[1:3]Out[]: [3, 5]

A slice

In []: p[0:-1]Out[]: [2, 3, 5]In []: p[::2]Out[]: [2, 5]

list[initial:final:step]FOSSEE (IIT Bombay) Plotting with Python 12 / 34

Page 13: Session2

Lists

List operations

In []: b = [ 11, 13, 17]In []: c = p + b

In []: cOut[]: [2, 3, 5, 7, 11, 13, 17]

In []: p.append(11)In []: pOut[]: [ 2, 3, 5, 7, 11]

FOSSEE (IIT Bombay) Plotting with Python 13 / 34

Page 14: Session2

Simple Pendulum

Outline

1 Plotting Points

2 Lists

3 Simple Pendulum

4 Strings

5 Summary

FOSSEE (IIT Bombay) Plotting with Python 14 / 34

Page 15: Session2

Simple Pendulum

Simple Pendulum - L and T

Let us look at the Simple Pendulum experiment.

L T T 2

0.1 0.690.2 0.900.3 1.190.4 1.300.5 1.470.6 1.580.7 1.770.8 1.830.9 1.94

LαT 2

FOSSEE (IIT Bombay) Plotting with Python 15 / 34

Page 16: Session2

Simple Pendulum

Lets use lists

In []: l = [0.1, 0.2, 0.3, 0.4, 0.5,0.6, 0.7, 0.8, 0.9]

In []: t = [0.69, 0.90, 1.19,1.30, 1.47, 1.58,1.77, 1.83, 1.94]

FOSSEE (IIT Bombay) Plotting with Python 16 / 34

Page 17: Session2

Simple Pendulum

Plotting L vs T 2

We must square each of the values in tHow to do it?We use a for loop to iterate over t

FOSSEE (IIT Bombay) Plotting with Python 17 / 34

Page 18: Session2

Simple Pendulum

Plotting L vs T 2

In []: tsq = []

In []: len(l)Out[]: 9

In []: len(t)Out[]: 9

In []: for time in t:....: tsq.append(time*time)....:....:

In []: plot(l, tsq)Out[]: [<matplotlib.lines.Line2D object at 0xa5b05ac>]

This gives tsq which is the list of squares of t values.

FOSSEE (IIT Bombay) Plotting with Python 18 / 34

Page 19: Session2

Simple Pendulum

How to come out of the for loop?

Hitting the “ENTER” key twice returns the cursor to theprevious indentation level

In []: for time in t:....: tsq.append(time*time)....:....:

In []: print tsq, len(tsq)

FOSSEE (IIT Bombay) Plotting with Python 19 / 34

Page 20: Session2

Simple Pendulum

FOSSEE (IIT Bombay) Plotting with Python 20 / 34

Page 21: Session2

Simple Pendulum

What about larger data sets?Data is usually present in a file!Lets look at the pendulum.txt file.$ cat pendulum.txt1.0000e-01 6.9004e-011.1000e-01 6.9497e-011.2000e-01 7.4252e-011.3000e-01 7.5360e-011.4000e-01 8.3568e-011.5000e-01 8.6789e-01

. . .Windows users:C:> type pendulum.txt

FOSSEE (IIT Bombay) Plotting with Python 21 / 34

Page 22: Session2

Simple Pendulum

Reading pendulum.txt

Let us generate a plot from the data fileFile contains L vs. T valuesL - Column1; T - Column2

FOSSEE (IIT Bombay) Plotting with Python 22 / 34

Page 23: Session2

Simple Pendulum

Plotting from pendulum.txt

Open a new script and type the following:

l = []t = []for line in open(’pendulum.txt’):

point = line.split()l.append(float(point[0]))t.append(float(point[1]))

tsq = []for time in t:

tsq.append(time*time)plot(l, tsq, ’.’)

FOSSEE (IIT Bombay) Plotting with Python 23 / 34

Page 24: Session2

Simple Pendulum

Save and run

Save as pendulum_plot.py.Run using %run -i pendulum_plot.py

FOSSEE (IIT Bombay) Plotting with Python 24 / 34

Page 25: Session2

Simple Pendulum

FOSSEE (IIT Bombay) Plotting with Python 25 / 34

Page 26: Session2

Simple Pendulum

Reading files . . .

for line in open(’pendulum.txt’):

opening file ‘pendulum.txt’reading the file line by lineline is a string

FOSSEE (IIT Bombay) Plotting with Python 26 / 34

Page 27: Session2

Strings

Outline

1 Plotting Points

2 Lists

3 Simple Pendulum

4 Strings

5 Summary

FOSSEE (IIT Bombay) Plotting with Python 27 / 34

Page 28: Session2

Strings

Strings

Anything within “quotes” is a string!

’ This is a string ’" This too! """" This one too! """’’’ And one more! ’’’

FOSSEE (IIT Bombay) Plotting with Python 28 / 34

Page 29: Session2

Strings

Strings and split()

In []: greet = ’hello world’

In []: greet.split()Out[]: [’hello’, ’world’]

This is what happens with line

In []: line = ’1.20 7.42’

In []: point = line.split()

In []: pointOut[]: [’1.20’, ’7.42’]

FOSSEE (IIT Bombay) Plotting with Python 29 / 34

Page 30: Session2

Strings

Getting floats from strings

In []: type(point[0])Out[]: <type ’str’>

But, we need floating point numbers

In []: t = float(point[0])

In []: type(t)Out[]: <type ’float’>

FOSSEE (IIT Bombay) Plotting with Python 30 / 34

Page 31: Session2

Strings

Let’s review the code

l = []t = []for line in open(’pendulum.txt’):

point = line.split()l.append(float(point[0]))t.append(float(point[1]))

tsq = []for time in t:

tsq.append(time*time)plot(l, tsq, ’.’)

FOSSEE (IIT Bombay) Plotting with Python 31 / 34

Page 32: Session2

Strings

FOSSEE (IIT Bombay) Plotting with Python 32 / 34

Page 33: Session2

Summary

Outline

1 Plotting Points

2 Lists

3 Simple Pendulum

4 Strings

5 Summary

FOSSEE (IIT Bombay) Plotting with Python 33 / 34

Page 34: Session2

Summary

What did we learn?

Plotting pointsPlot attributesListsfor

Reading filesTokenizingStrings

FOSSEE (IIT Bombay) Plotting with Python 34 / 34