day1/session2.tex
branchscipyin2010
changeset 441 9d9e4026238f
parent 426 7d8738ce004d
equal deleted inserted replaced
440:8839caef9f83 441:9d9e4026238f
    76 \title[Plotting with Python]{Python for Science and Engg: Plotting experimental data}
    76 \title[Plotting with Python]{Python for Science and Engg: Plotting experimental data}
    77 
    77 
    78 \author[FOSSEE group] {FOSSEE}
    78 \author[FOSSEE group] {FOSSEE}
    79 
    79 
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \date[] {SciPy 2010, Introductory tutorials,\\Day 1, Session 2}
    81 \date[] {SciPy.in 2010, Tutorials}
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    83 
    83 
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    85 %\logo{\pgfuseimage{iitmlogo}}
    85 %\logo{\pgfuseimage{iitmlogo}}
    86 
    86 
   131 \begin{lstlisting}
   131 \begin{lstlisting}
   132 In []: time = [0, 1, 2, 3]
   132 In []: time = [0, 1, 2, 3]
   133 
   133 
   134 In []: distance = [7, 11, 15, 19]
   134 In []: distance = [7, 11, 15, 19]
   135 
   135 
   136 In []: plot(time,distance)
   136 In []: plot(time, distance)
   137 Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>]
   137 Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>]
   138 
   138 
   139 In []: xlabel('time')
   139 In []: xlabel('time')
   140 Out[]: <matplotlib.text.Text object at 0x986e9ac>
   140 Out[]: <matplotlib.text.Text object at 0x986e9ac>
   141 
   141 
   217 Out[]: 12
   217 Out[]: 12
   218 \end{lstlisting}
   218 \end{lstlisting}
   219 \end{frame}
   219 \end{frame}
   220 
   220 
   221 \begin{frame}[fragile]
   221 \begin{frame}[fragile]
   222   \frametitle{List: Slicing}
   222 \frametitle{List: Appending elements}
   223   \begin{block}{Remember\ldots}
   223 \begin{lstlisting}
   224 	\kwrd{In []: p = [ 2, 3, 5, 7]}
       
   225   \end{block}
       
   226 \begin{lstlisting}
       
   227 In []: p[1:3]
       
   228 Out[]: [3, 5]
       
   229 \end{lstlisting}
       
   230 \emphbar{A slice}
       
   231 \begin{lstlisting}
       
   232 In []: p[0:-1]
       
   233 Out[]: [2, 3, 5]
       
   234 In []: p[::2]
       
   235 Out[]: [2, 5]
       
   236 \end{lstlisting}
       
   237 \alert{\typ{list[initial:final:step]}}
       
   238 \end{frame}
       
   239 
       
   240 \begin{frame}[fragile]
       
   241   \frametitle{List: Slicing}
       
   242   What is the output of the following?
       
   243 \begin{lstlisting}
       
   244 In []: p[1::2]
       
   245 
       
   246 In []: p[1:-1:2]
       
   247 \end{lstlisting}
       
   248 \end{frame}
       
   249 
       
   250 
       
   251 %% more on list slicing
       
   252 \begin{frame}[fragile]
       
   253 \frametitle{List operations}
       
   254 \begin{lstlisting}
       
   255 In []: b = [ 11, 13, 17]
       
   256 In []: c = p + b
       
   257 
       
   258 In []: c
       
   259 Out[]: [2, 3, 5, 7, 11, 13, 17]
       
   260 
       
   261 In []: p.append(11)
   224 In []: p.append(11)
   262 In []: p
   225 In []: p
   263 Out[]: [ 2, 3, 5, 7, 11]
   226 Out[]: [ 2, 3, 5, 7, 11]
   264 \end{lstlisting}
   227 
   265 Question: Does \typ{c} change now that \typ{p} is changed?
   228 In []: b = [11, 13, 17]
       
   229 In []: p.append(b)
       
   230 Out[]: [ 2, 3, 5, 7, 11, [11, 13, 17]]
       
   231 \end{lstlisting}
   266 %\inctime{10}
   232 %\inctime{10}
   267 \end{frame}
   233 \end{frame}
   268 
   234 
   269 \section{Simple Pendulum}
   235 \section{Simple Pendulum}
   270 \begin{frame}[fragile]
   236 \begin{frame}[fragile]
   286 0.9 & 1.94 & \\ \hline
   252 0.9 & 1.94 & \\ \hline
   287 \end{tabular}
   253 \end{tabular}
   288 \end{small}\\
   254 \end{small}\\
   289 \alert{$L \alpha T^2$}
   255 \alert{$L \alpha T^2$}
   290 \end{center}
   256 \end{center}
   291 \end{frame}
   257 Our data is present in \typ{pendulum.txt}. Let's look at it.
   292 
       
   293 \begin{frame}[fragile]
       
   294 \frametitle{Lets use lists}
       
   295 \begin{lstlisting}
       
   296 In []: L = [0.1, 0.2, 0.3, 0.4, 0.5, 
       
   297             0.6, 0.7, 0.8, 0.9]
       
   298 
       
   299 In []: t = [0.69, 0.90, 1.19, 
       
   300             1.30, 1.47, 1.58, 
       
   301             1.77, 1.83, 1.94]
       
   302 \end{lstlisting}
       
   303 \alert{Gotcha}: Make sure \typ{L} and \typ{t} have the same number
       
   304 of elements
       
   305 
       
   306 \begin{lstlisting}
       
   307 In []: print len(L), len(t)
       
   308 \end{lstlisting}
       
   309 
       
   310 \end{frame}
       
   311 
       
   312 \begin{frame}[fragile]
       
   313 \frametitle{Plotting $L$ vs $T^2$}
       
   314 \begin{itemize}
       
   315 \item We must square each of the values in \typ{t}
       
   316 \item How do we do it?
       
   317 \item We use a \kwrd{for} loop to iterate over \typ{t}
       
   318 \end{itemize}
       
   319 \end{frame}
       
   320 
       
   321 \begin{frame}[fragile]
       
   322 \frametitle{Plotting $L$ vs $T^2$}
       
   323 \begin{lstlisting}
       
   324 In []: tsq = []
       
   325 
       
   326 In []: for time in t:
       
   327  ....:     tsq.append(time*time)
       
   328  ....:
       
   329  ....:
       
   330 
       
   331 \end{lstlisting}
       
   332 This gives \typ{tsq} which is the list of squares of \typ{t} values.
       
   333 \begin{lstlisting}
       
   334 In []: print len(L), len(t), len(tsq)
       
   335 Out[]: 9 9 9
       
   336 \end{lstlisting}
       
   337 \end{frame}
       
   338 
       
   339 \begin{frame}[fragile]
       
   340   \frametitle{How do you exit the \texttt{for} loop?}
       
   341   Hitting the ``ENTER'' key twice returns the cursor to the previous indentation level
       
   342   \begin{lstlisting}
       
   343     In []: for time in t:
       
   344      ....:     tsq.append(time*time)
       
   345      ....:     
       
   346      ....:     
       
   347 
       
   348     In []: plot(L, tsq)
       
   349   \end{lstlisting}
       
   350 \end{frame}
       
   351 
       
   352 \begin{frame}[fragile]
       
   353 \begin{figure}
       
   354 \includegraphics[width=3.5in]{data/L-TSq-limited.png}
       
   355 \end{figure}
       
   356 \end{frame}
       
   357 
       
   358 \begin{frame}[fragile]
       
   359 \frametitle{What about larger data sets?}
       
   360 \alert{Data is usually present in a file!} \\
       
   361 Lets look at the \typ{pendulum.txt} file.
       
   362 \begin{lstlisting} 
       
   363 In []: cat pendulum.txt 
       
   364 1.0000e-01 6.9004e-01
       
   365 1.1000e-01 6.9497e-01
       
   366 1.2000e-01 7.4252e-01
       
   367 1.3000e-01 7.5360e-01
       
   368 \end{lstlisting}  %$
       
   369 \ldots
       
   370 \end{frame}
       
   371 
       
   372 \begin{frame}[fragile]
       
   373 \frametitle{Reading \typ{pendulum.txt}}
       
   374 \begin{itemize}
       
   375   \item File contains L vs.\ T values 
       
   376   \item First Column - L values
       
   377   \item Second Column - T values
       
   378   \item Let us generate a plot from the data file
       
   379 \end{itemize}
       
   380 \end{frame}
   258 \end{frame}
   381 
   259 
   382 \begin{frame}[fragile]
   260 \begin{frame}[fragile]
   383     \frametitle{Gotcha and an aside}
   261     \frametitle{Gotcha and an aside}
   384     Ensure you are in the same directory as \typ{pendulum.txt}\\
   262     Ensure you are in the same directory as \typ{pendulum.txt}\\
   396     \begin{lstlisting}
   274     \begin{lstlisting}
   397 In []: ?
   275 In []: ?
   398     \end{lstlisting}
   276     \end{lstlisting}
   399 \end{frame}
   277 \end{frame}
   400 
   278 
   401 
   279 \begin{frame}[fragile]
   402 \begin{frame}[fragile]
   280 \frametitle{Looking at \typ{pendulum.txt}}
   403 \frametitle{Plotting from \typ{pendulum.txt}}
   281 \begin{lstlisting} 
   404 Open a new script and save as \typ{pendulum_plot.py}
   282 In []: cat pendulum.txt 
   405 \begin{lstlisting}
   283 1.0000e-01 6.9004e-01
   406 L = []
   284 1.1000e-01 6.9497e-01
   407 t = []
   285 1.2000e-01 7.4252e-01
   408 for line in open('pendulum.txt'):
   286 1.3000e-01 7.5360e-01
   409     point = line.split()
   287 \end{lstlisting}  %$
   410     L.append(float(point[0]))
   288 \ldots
   411     t.append(float(point[1]))
       
   412 tsq = []
       
   413 for time in t:
       
   414     tsq.append(time*time)
       
   415 plot(L, tsq, '.')
       
   416 \end{lstlisting}
       
   417 \end{frame}
       
   418 
       
   419 \begin{frame}
       
   420 \frametitle{Save and run}
       
   421 \begin{itemize}
   289 \begin{itemize}
   422     \item Save as \typ{pendulum\_plot.py}
   290   \item File contains L vs.\ T values 
   423   \item Run using \kwrd{\%run -i pendulum\_plot.py}
   291   \item First Column -- L values
       
   292   \item Second Column -- T values
   424 \end{itemize}
   293 \end{itemize}
   425 \end{frame}
   294 \end{frame}
   426 
   295 
   427 \begin{frame}[fragile]
   296 \begin{frame}[fragile]
       
   297   \frametitle{\typ{loadtxt}}
       
   298   \begin{itemize}
       
   299   \item We shall use the \typ{loadtxt} command to load data
       
   300   \item Let's use \typ{primes.txt} file learn to use it
       
   301   \item \typ{primes.txt} has a single column
       
   302   \item Then, we shall use it for \typ{pendulum.txt} -- 2 cols
       
   303   \end{itemize}
       
   304 \end{frame}
       
   305 
       
   306 \begin{frame}[fragile]
       
   307 \frametitle{What's in \typ{primes.txt}?}
       
   308 Lets look at the \typ{primes.txt} file.
       
   309 \begin{lstlisting} 
       
   310 In []: cat primes.txt 
       
   311 2
       
   312 3      
       
   313 5      
       
   314 7     
       
   315 11     
       
   316 13
       
   317 .
       
   318 .
       
   319 .
       
   320 \end{lstlisting}  %$
       
   321 \end{frame}
       
   322 
       
   323 
       
   324 \begin{frame}[fragile]
       
   325   \frametitle{\typ{loadtxt} \ldots}
       
   326   \begin{lstlisting}
       
   327 In []: primes = loadtxt('primes.txt')
       
   328 In []: primes
       
   329 Out[]: array([ 2., 3., 5., 7., 11., 13., 
       
   330                17., 19., 23., 29., 31., 
       
   331                37., 41., 43., 47., 53., 
       
   332                59., 61., 67., 71., 73., 
       
   333                79., 83., 89., 97.])
       
   334   \end{lstlisting}
       
   335 
       
   336 \end{frame}
       
   337 
       
   338 \begin{frame}[fragile]
       
   339   \frametitle{Reading \typ{pendulum.txt}}
       
   340   \begin{lstlisting}
       
   341 In []: pend = loadtxt('pendulum.txt')
       
   342 In []: pend
       
   343   \end{lstlisting}
       
   344   \begin{itemize}
       
   345   \item \typ{pend} is 2 Dimensional. 
       
   346   \item We don't \alert{yet} know how to handle it.
       
   347   \item We obtain 1D sequences using \typ{unpack=True}
       
   348   \end{itemize}
       
   349 
       
   350   \begin{lstlisting}
       
   351 In []: L, T = loadtxt('pendulum.txt', 
       
   352                       unpack=True)
       
   353 In []: print L, T
       
   354 In []: print len(L), len(T)
       
   355 Out[]: 90 90
       
   356   \end{lstlisting}
       
   357 \end{frame}
       
   358 
       
   359 \begin{frame}[fragile]
       
   360 \frametitle{Plotting $L$ vs $T^2$}
       
   361 \begin{itemize}
       
   362 \item We must square each of the values in \typ{T}
       
   363 \item How to do it?
       
   364 \item We use a \kwrd{for} loop to iterate over \typ{T}
       
   365 \end{itemize}
       
   366 \end{frame}
       
   367 
       
   368 \begin{frame}[fragile]
       
   369 \frametitle{Plotting $L$ vs $T^2$}
       
   370 \begin{lstlisting}
       
   371 In []: tsq = []
       
   372 
       
   373 In []: for time in T:
       
   374  ....:     tsq.append(time*time)
       
   375  ....:
       
   376  ....:
       
   377 
       
   378 \end{lstlisting}
       
   379 \alert{Hit ``ENTER'' key twice, to get out of \typ{for} loop}.
       
   380 
       
   381 \begin{lstlisting}
       
   382 In []: print len(tsq)
       
   383 Out[]: 90
       
   384 \end{lstlisting}
       
   385 \typ{tsq} is a \kwrd{list} of squares of \typ{T} values.
       
   386 \end{frame}
       
   387 
       
   388 
       
   389 \begin{frame}[fragile]
       
   390 \frametitle{Plotting $L$ vs $T^2$ \ldots}
       
   391 
       
   392 \begin{lstlisting}
       
   393 In []: plot(L, tsq, '.')
       
   394 \end{lstlisting}
       
   395 
   428 \begin{figure}
   396 \begin{figure}
   429 \includegraphics[width=3.5in]{data/L-Tsq.png}
   397   \includegraphics[width=3.5in]{data/L-Tsq.png}
   430 \end{figure}
   398 \end{figure}
   431 \end{frame}
   399 
   432 
       
   433 \begin{frame}[fragile]
       
   434   \frametitle{Reading files \ldots}
       
   435 \typ{for line in open('pendulum.txt'):}
       
   436 \begin{itemize}
       
   437 \item opening file `\typ{pendulum.txt}'
       
   438 \item reading the file line by line
       
   439 \item \typ{line} is a \kwrd{string}
       
   440 \end{itemize}
       
   441 \end{frame}
       
   442 
       
   443 \section{Strings}
       
   444 
       
   445 \begin{frame}[fragile]
       
   446 \frametitle{Strings}
       
   447 Anything within ``quotes'' is a string!
       
   448 \begin{lstlisting}
       
   449 ' This is a string '  
       
   450 " This too! "
       
   451 """ This one too! """
       
   452 ''' And one more! '''
       
   453 \end{lstlisting}
       
   454 \end{frame}
       
   455 
       
   456 \begin{frame}[fragile]
       
   457 \frametitle{Strings}
       
   458 Why so many?
       
   459 \begin{lstlisting}
       
   460 ' "Do or do not.  No try." said Yoda.'  
       
   461 " ' is a mighty lonely quote."
       
   462 \end{lstlisting}
       
   463 The triple quoted ones can span multiple lines!
       
   464 
       
   465 \begin{lstlisting}
       
   466 """ The quick brown
       
   467 fox jumped over
       
   468     the lazy dingbat. 
       
   469 """
       
   470 \end{lstlisting}
       
   471 \end{frame}
       
   472 
       
   473 \begin{frame}[fragile]
       
   474 \frametitle{Strings and \typ{split()}}
       
   475   \begin{lstlisting}
       
   476 In []: greet = 'hello world'
       
   477 
       
   478 In []: greet.split()
       
   479 Out[]: ['hello', 'world']
       
   480   \end{lstlisting}
       
   481 This is what happens with \typ{line}
       
   482   \begin{lstlisting}
       
   483 In []: line = '1.20 7.42'
       
   484 
       
   485 In []: point = line.split()
       
   486 
       
   487 In []: point
       
   488 Out[]: ['1.20', '7.42']
       
   489   \end{lstlisting}
       
   490 \end{frame}
       
   491 
       
   492 \begin{frame}[fragile]
       
   493 \frametitle{Getting floats from strings}
       
   494   \begin{lstlisting}
       
   495 In []: type(point[0])
       
   496 Out[]: <type 'str'>
       
   497   \end{lstlisting}
       
   498 But, we need floating point numbers
       
   499   \begin{lstlisting}
       
   500 In []: t = float(point[0])
       
   501 
       
   502 In []: type(t)
       
   503 Out[]: <type 'float'>
       
   504   \end{lstlisting}
       
   505 \end{frame}
       
   506 
       
   507 \begin{frame}[fragile]
       
   508 \frametitle{Let's review the code}
       
   509 \begin{lstlisting}
       
   510 L = []
       
   511 t = []
       
   512 for line in open('pendulum.txt'):
       
   513     point = line.split()
       
   514     L.append(float(point[0]))
       
   515     t.append(float(point[1]))
       
   516 tsq = []
       
   517 for time in t:
       
   518     tsq.append(time*time)
       
   519 plot(L, tsq, '.')
       
   520 \end{lstlisting}
       
   521 \end{frame}
       
   522 
       
   523 \begin{frame}[fragile]
       
   524 \begin{figure}
       
   525 \includegraphics[width=3.5in]{data/L-Tsq.png}
       
   526 \end{figure}
       
   527 \end{frame}
   400 \end{frame}
   528 
   401 
   529 \section {Summary}
   402 \section {Summary}
   530 \begin{frame}[fragile]
   403 \begin{frame}[fragile]
   531 \frametitle{What did we learn?}
   404 \frametitle{What did we learn?}
   532 \begin{itemize}
   405 \begin{itemize}
   533   \item Plot attributes and plotting points
   406   \item Plot attributes and plotting points
   534   \item Lists
   407   \item Lists
   535   \item \kwrd{for}
   408   \item \kwrd{for}
   536   \item Reading files
   409   \item Loading data from files
   537   \item Tokenizing
       
   538   \item Strings
       
   539 \end{itemize}
   410 \end{itemize}
   540 \end{frame}
   411 \end{frame}
   541 
   412 
   542 \end{document}
   413 \end{document}
   543 
   414