SlideShare una empresa de Scribd logo
1 de 34
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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
 

Similar a Session2

Session1
Session1Session1
Session1vattam
 
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 rateIgor Sfiligoi
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
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 RewritingEelco Visser
 
Python for text processing
Python for text processingPython for text processing
Python for text processingXiang Li
 
Python For Scientists
Python For ScientistsPython For Scientists
Python For Scientistsaeberspaecher
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptxRamiHarrathi1
 
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 RewritingEelco Visser
 
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).pptxjovannyflex
 
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).pptxjovannyflex
 
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 SciPyKimikazu Kato
 
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 02Fariz Darari
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3Abdul Haseeb
 
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
 

Similar a 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...
 

Último

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
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.pdfAdmir Softic
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
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 FellowsMebane Rash
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
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 ConsultingTechSoup
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Último (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
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
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.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
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

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