Session2
Upcoming SlideShare
Loading in...5
×
 

Session2

on

  • 626 views

 

Statistics

Views

Total Views
626
Views on SlideShare
626
Embed Views
0

Actions

Likes
0
Downloads
11
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Session2 Session2 Presentation Transcript

  • 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
  • Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 2 / 34
  • Plotting Points Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 3 / 34
  • 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 0xa73a 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
  • Plotting Points Is this what you have? FOSSEE (IIT Bombay) Plotting with Python 5 / 34
  • Plotting Points Plotting points What if we want to plot the points! In []: clf() In []: plot(x, y, ’o’) Out[]: [<matplotlib.lines.Line2D object In []: clf() In []: plot(x, y, ’.’) Out[]: [<matplotlib.lines.Line2D object FOSSEE (IIT Bombay) Plotting with Python 6 / 34
  • Plotting Points FOSSEE (IIT Bombay) Plotting with Python 7 / 34
  • Plotting Points Additional Plotting Attributes ’o’ - Filled circles ’.’ - Small Dots ’-’ - Lines ’- -’ - Dashed lines FOSSEE (IIT Bombay) Plotting with Python 8 / 34
  • Lists Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 9 / 34
  • 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
  • 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
  • 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
  • Lists List operations In []: b = [ 11, 13, 17] In []: c = p + b In []: c Out[]: [2, 3, 5, 7, 11, 13, 17] In []: p.append(11) In []: p Out[]: [ 2, 3, 5, 7, 11] FOSSEE (IIT Bombay) Plotting with Python 13 / 34
  • Simple Pendulum Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 14 / 34
  • Simple Pendulum Simple Pendulum - L and T Let us look at the Simple Pendulum experiment. L T T2 0.1 0.69 0.2 0.90 0.3 1.19 0.4 1.30 0.5 1.47 0.6 1.58 0.7 1.77 0.8 1.83 0.9 1.94 LαT 2 FOSSEE (IIT Bombay) Plotting with Python 15 / 34
  • 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
  • Simple Pendulum Plotting L vs T 2 We must square each of the values in t How to do it? We use a for loop to iterate over t FOSSEE (IIT Bombay) Plotting with Python 17 / 34
  • 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) ....: ....: FOSSEE (IIT Bombay) Plotting with Python 18 / 34
  • Simple Pendulum How to come out of the for loop? Hitting the “ENTER” key twice returns the cursor to the previous indentation level In []: for time in t: ....: tsq.append(time*time) ....: ....: In []: print tsq, len(tsq) FOSSEE (IIT Bombay) Plotting with Python 19 / 34
  • Simple Pendulum FOSSEE (IIT Bombay) Plotting with Python 20 / 34
  • Simple Pendulum What about larger data sets? Data is usually present in a file! Lets look at the pendulum.txt file. $ cat pendulum.txt 1.0000e-01 6.9004e-01 1.1000e-01 6.9497e-01 1.2000e-01 7.4252e-01 1.3000e-01 7.5360e-01 1.4000e-01 8.3568e-01 1.5000e-01 8.6789e-01 ... Windows users: C:> type pendulum.txt FOSSEE (IIT Bombay) Plotting with Python 21 / 34
  • Simple Pendulum Reading pendulum.txt Let us generate a plot from the data file File contains L vs. T values L - Column1; T - Column2 FOSSEE (IIT Bombay) Plotting with Python 22 / 34
  • 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
  • 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
  • Simple Pendulum FOSSEE (IIT Bombay) Plotting with Python 25 / 34
  • Simple Pendulum Reading files . . . for line in open(’pendulum.txt’): opening file ‘pendulum.txt’ reading the file line by line line is a string FOSSEE (IIT Bombay) Plotting with Python 26 / 34
  • Strings Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 27 / 34
  • 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
  • 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 []: point Out[]: [’1.20’, ’7.42’] FOSSEE (IIT Bombay) Plotting with Python 29 / 34
  • 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
  • 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
  • Strings FOSSEE (IIT Bombay) Plotting with Python 32 / 34
  • Summary Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 33 / 34
  • Summary What did we learn? Plotting points Plot attributes Lists for Reading files Tokenizing Strings FOSSEE (IIT Bombay) Plotting with Python 34 / 34