Session2 thiet ke_gia_cong_trung_tam_tien_phay_nhieu_truc_multi_task_machining
Session2
Transcript of Session2
![Page 1: Session2](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/2.jpg)
Outline
1 Plotting Points
2 Lists
3 Simple Pendulum
4 Strings
5 Summary
FOSSEE (IIT Bombay) Plotting with Python 2 / 34
![Page 3: Session2](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/5.jpg)
Plotting Points
Is this what you have?FOSSEE (IIT Bombay) Plotting with Python 5 / 34
![Page 6: Session2](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/7.jpg)
Plotting Points
FOSSEE (IIT Bombay) Plotting with Python 7 / 34
![Page 8: Session2](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/8.jpg)
Plotting Points
Additional Plotting Attributes
’o’ - Filled circles’.’ - Small Dots’-’ - Lines’- -’ - Dashed lines
FOSSEE (IIT Bombay) Plotting with Python 8 / 34
![Page 9: Session2](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/20.jpg)
Simple Pendulum
FOSSEE (IIT Bombay) Plotting with Python 20 / 34
![Page 21: Session2](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/25.jpg)
Simple Pendulum
FOSSEE (IIT Bombay) Plotting with Python 25 / 34
![Page 26: Session2](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/32.jpg)
Strings
FOSSEE (IIT Bombay) Plotting with Python 32 / 34
![Page 33: Session2](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/33.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022060115/557ce3b5d8b42ae6078b465f/html5/thumbnails/34.jpg)
Summary
What did we learn?
Plotting pointsPlot attributesListsfor
Reading filesTokenizingStrings
FOSSEE (IIT Bombay) Plotting with Python 34 / 34