SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

C Programming: Pointer (Examples)
C Programming: Pointer (Examples)C Programming: Pointer (Examples)
C Programming: Pointer (Examples)
 
NUMPY
NUMPY NUMPY
NUMPY
 
Cs2251 daa
Cs2251 daaCs2251 daa
Cs2251 daa
 
Computer science sqp
Computer science sqpComputer science sqp
Computer science sqp
 
Adobe
AdobeAdobe
Adobe
 
Parallel search
Parallel searchParallel search
Parallel search
 
Ch2
Ch2Ch2
Ch2
 
Cis068 08
Cis068 08Cis068 08
Cis068 08
 
Lz77 (sliding window)
Lz77 (sliding window)Lz77 (sliding window)
Lz77 (sliding window)
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
LZ78
LZ78LZ78
LZ78
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Ch8b
Ch8bCh8b
Ch8b
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
See through C
See through CSee through C
See through C
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Algorithms presentation on Path Matrix, Bell Number and Sorting
Algorithms presentation on Path Matrix, Bell Number and SortingAlgorithms presentation on Path Matrix, Bell Number and Sorting
Algorithms presentation on Path Matrix, Bell Number and Sorting
 
Intoduction to numpy
Intoduction to numpyIntoduction to numpy
Intoduction to numpy
 
Pointers
PointersPointers
Pointers
 

Ähnlich wie Session2

Session1
Session1Session1
Session1
vattam
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
g3_nittala
 
Python for text processing
Python for text processingPython for text processing
Python for text processing
Xiang Li
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 

Ähnlich wie Session2 (20)

Session1
Session1Session1
Session1
 
Speeding up bowtie2 by improving cache-hit rate
Speeding up bowtie2 by improving cache-hit rateSpeeding up bowtie2 by improving cache-hit rate
Speeding up bowtie2 by improving cache-hit rate
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
Python for Dummies
Python for DummiesPython for Dummies
Python for Dummies
 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
 
Python for text processing
Python for text processingPython for text processing
Python for text processing
 
Python For Scientists
Python For ScientistsPython For Scientists
Python For Scientists
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Linq inside out
Linq inside outLinq inside out
Linq inside out
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 

Kürzlich hochgeladen

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Kürzlich hochgeladen (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 

Session2

  • 1. 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
  • 2. Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 2 / 34
  • 3. Plotting Points Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 3 / 34
  • 4. 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
  • 5. Plotting Points Is this what you have? FOSSEE (IIT Bombay) Plotting with Python 5 / 34
  • 6. 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
  • 7. Plotting Points FOSSEE (IIT Bombay) Plotting with Python 7 / 34
  • 8. Plotting Points Additional Plotting Attributes ’o’ - Filled circles ’.’ - Small Dots ’-’ - Lines ’- -’ - Dashed lines FOSSEE (IIT Bombay) Plotting with Python 8 / 34
  • 9. Lists Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 9 / 34
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. Simple Pendulum Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 14 / 34
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. Simple Pendulum FOSSEE (IIT Bombay) Plotting with Python 20 / 34
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. Simple Pendulum FOSSEE (IIT Bombay) Plotting with Python 25 / 34
  • 26. 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
  • 27. Strings Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 27 / 34
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. Strings FOSSEE (IIT Bombay) Plotting with Python 32 / 34
  • 33. Summary Outline 1 Plotting Points 2 Lists 3 Simple Pendulum 4 Strings 5 Summary FOSSEE (IIT Bombay) Plotting with Python 33 / 34
  • 34. Summary What did we learn? Plotting points Plot attributes Lists for Reading files Tokenizing Strings FOSSEE (IIT Bombay) Plotting with Python 34 / 34