day1/session2.tex
branchscipy2010
changeset 408 217c38c06ebd
parent 397 28915381ac32
child 426 7d8738ce004d
equal deleted inserted replaced
407:b5d3b5ddac7b 408:217c38c06ebd
    73 
    73 
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    75 % Title page
    75 % Title page
    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] {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[] {30 April, 2010\\Day 1, Session 2}
    81 \date[] {SciPy 2010, Introductory tutorials,\\Day 1, Session 2}
    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 
   153 \end{frame}
   153 \end{frame}
   154 
   154 
   155 \begin{frame}[fragile]
   155 \begin{frame}[fragile]
   156 \frametitle{Plotting points}
   156 \frametitle{Plotting points}
   157 \begin{itemize}
   157 \begin{itemize}
   158 \item What if we want to plot the points!
   158 \item What if we want to plot the points?
   159 \end{itemize}
   159 \end{itemize}
   160 \begin{lstlisting}
   160 \begin{lstlisting}
   161   In []: clf()
   161   In []: clf()
   162 
   162 
   163   In []: plot(time, distance, 'o')
   163   In []: plot(time, distance, 'o')
   235 Out[]: [2, 5]
   235 Out[]: [2, 5]
   236 \end{lstlisting}
   236 \end{lstlisting}
   237 \alert{\typ{list[initial:final:step]}}
   237 \alert{\typ{list[initial:final:step]}}
   238 \end{frame}
   238 \end{frame}
   239 
   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 
   240 %% more on list slicing
   251 %% more on list slicing
   241 \begin{frame}[fragile]
   252 \begin{frame}[fragile]
   242 \frametitle{List operations}
   253 \frametitle{List operations}
   243 \begin{lstlisting}
   254 \begin{lstlisting}
   244 In []: b = [ 11, 13, 17]
   255 In []: b = [ 11, 13, 17]
   249 
   260 
   250 In []: p.append(11)
   261 In []: p.append(11)
   251 In []: p
   262 In []: p
   252 Out[]: [ 2, 3, 5, 7, 11]
   263 Out[]: [ 2, 3, 5, 7, 11]
   253 \end{lstlisting}
   264 \end{lstlisting}
       
   265 Question: Does \typ{c} change now that \typ{p} is changed?
   254 %\inctime{10}
   266 %\inctime{10}
   255 \end{frame}
   267 \end{frame}
   256 
   268 
   257 \section{Simple Pendulum}
   269 \section{Simple Pendulum}
   258 \begin{frame}[fragile]
   270 \begin{frame}[fragile]
   286 
   298 
   287 In []: t = [0.69, 0.90, 1.19, 
   299 In []: t = [0.69, 0.90, 1.19, 
   288             1.30, 1.47, 1.58, 
   300             1.30, 1.47, 1.58, 
   289             1.77, 1.83, 1.94]
   301             1.77, 1.83, 1.94]
   290 \end{lstlisting}
   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 
   291 \end{frame}
   310 \end{frame}
   292 
   311 
   293 \begin{frame}[fragile]
   312 \begin{frame}[fragile]
   294 \frametitle{Plotting $L$ vs $T^2$}
   313 \frametitle{Plotting $L$ vs $T^2$}
   295 \begin{itemize}
   314 \begin{itemize}
   296 \item We must square each of the values in \typ{t}
   315 \item We must square each of the values in \typ{t}
   297 \item How to do it?
   316 \item How do we do it?
   298 \item We use a \kwrd{for} loop to iterate over \typ{t}
   317 \item We use a \kwrd{for} loop to iterate over \typ{t}
   299 \end{itemize}
   318 \end{itemize}
   300 \end{frame}
   319 \end{frame}
   301 
   320 
   302 \begin{frame}[fragile]
   321 \begin{frame}[fragile]
   308  ....:     tsq.append(time*time)
   327  ....:     tsq.append(time*time)
   309  ....:
   328  ....:
   310  ....:
   329  ....:
   311 
   330 
   312 \end{lstlisting}
   331 \end{lstlisting}
   313 This gives \kwrd{tsq} which is the list of squares of \typ{t} values.
   332 This gives \typ{tsq} which is the list of squares of \typ{t} values.
   314 \begin{lstlisting}
   333 \begin{lstlisting}
   315 In []: print len(L), len(t), len(tsq)
   334 In []: print len(L), len(t), len(tsq)
   316 Out[]: 9 9 9
   335 Out[]: 9 9 9
   317 \end{lstlisting}
   336 \end{lstlisting}
   318 \end{frame}
   337 \end{frame}
   319 
   338 
   320 \begin{frame}[fragile]
   339 \begin{frame}[fragile]
   321   \frametitle{How to come out of the \texttt{for} loop?}
   340   \frametitle{How do you exit the \texttt{for} loop?}
   322   Hitting the ``ENTER'' key twice returns the cursor to the previous indentation level
   341   Hitting the ``ENTER'' key twice returns the cursor to the previous indentation level
   323   \begin{lstlisting}
   342   \begin{lstlisting}
   324     In []: for time in t:
   343     In []: for time in t:
   325      ....:     tsq.append(time*time)
   344      ....:     tsq.append(time*time)
   326      ....:     
   345      ....:     
   351 \end{frame}
   370 \end{frame}
   352 
   371 
   353 \begin{frame}[fragile]
   372 \begin{frame}[fragile]
   354 \frametitle{Reading \typ{pendulum.txt}}
   373 \frametitle{Reading \typ{pendulum.txt}}
   355 \begin{itemize}
   374 \begin{itemize}
   356   \item File contains L vs. T values 
   375   \item File contains L vs.\ T values 
   357   \item First Column - L values
   376   \item First Column - L values
   358   \item Second Column - T values
   377   \item Second Column - T values
   359   \item Let us generate a plot from the data file
   378   \item Let us generate a plot from the data file
   360 \end{itemize}
   379 \end{itemize}
   361 \end{frame}
   380 \end{frame}
   362 
   381 
   363 \begin{frame}[fragile]
   382 \begin{frame}[fragile]
       
   383     \frametitle{Gotcha and an aside}
       
   384     Ensure you are in the same directory as \typ{pendulum.txt}\\
       
   385     if not, do the following on IPython:
       
   386     \begin{lstlisting}
       
   387 In []: %cd directory_containing_file
       
   388 # Check if pendulum.txt is there.
       
   389 In []: ls
       
   390 # Also try 
       
   391 In []: !ls
       
   392     \end{lstlisting}
       
   393 
       
   394     \alert{Note:} \typ{\%cd} is an IPython magic command.  For more information
       
   395     do:
       
   396     \begin{lstlisting}
       
   397 In []: ?
       
   398     \end{lstlisting}
       
   399 \end{frame}
       
   400 
       
   401 
       
   402 \begin{frame}[fragile]
   364 \frametitle{Plotting from \typ{pendulum.txt}}
   403 \frametitle{Plotting from \typ{pendulum.txt}}
   365 Open a new script\\
   404 Open a new script and save as \typ{pendulum_plot.py}
   366 Save as \typ{pendulum_plot.py} after typing first line
       
   367 \begin{lstlisting}
   405 \begin{lstlisting}
   368 L = []
   406 L = []
   369 t = []
   407 t = []
   370 for line in open('pendulum.txt'):
   408 for line in open('pendulum.txt'):
   371     point = line.split()
   409     point = line.split()
   379 \end{frame}
   417 \end{frame}
   380 
   418 
   381 \begin{frame}
   419 \begin{frame}
   382 \frametitle{Save and run}
   420 \frametitle{Save and run}
   383 \begin{itemize}
   421 \begin{itemize}
   384   \item Save as pendulum\_plot.py.
   422     \item Save as \typ{pendulum\_plot.py}
   385   \item Run using \kwrd{\%run -i pendulum\_plot.py}
   423   \item Run using \kwrd{\%run -i pendulum\_plot.py}
   386 \end{itemize}
   424 \end{itemize}
   387 \end{frame}
   425 \end{frame}
   388 
   426 
   389 \begin{frame}[fragile]
   427 \begin{frame}[fragile]
   401 \item \typ{line} is a \kwrd{string}
   439 \item \typ{line} is a \kwrd{string}
   402 \end{itemize}
   440 \end{itemize}
   403 \end{frame}
   441 \end{frame}
   404 
   442 
   405 \section{Strings}
   443 \section{Strings}
       
   444 
   406 \begin{frame}[fragile]
   445 \begin{frame}[fragile]
   407 \frametitle{Strings}
   446 \frametitle{Strings}
   408 Anything within ``quotes'' is a string!
   447 Anything within ``quotes'' is a string!
   409 \begin{lstlisting}
   448 \begin{lstlisting}
   410 ' This is a string '  
   449 ' This is a string '  
   411 " This too! "
   450 " This too! "
   412 """ This one too! """
   451 """ This one too! """
   413 ''' And one more! '''
   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 """
   414 \end{lstlisting}
   470 \end{lstlisting}
   415 \end{frame}
   471 \end{frame}
   416 
   472 
   417 \begin{frame}[fragile]
   473 \begin{frame}[fragile]
   418 \frametitle{Strings and \typ{split()}}
   474 \frametitle{Strings and \typ{split()}}
   472 
   528 
   473 \section {Summary}
   529 \section {Summary}
   474 \begin{frame}[fragile]
   530 \begin{frame}[fragile]
   475 \frametitle{What did we learn?}
   531 \frametitle{What did we learn?}
   476 \begin{itemize}
   532 \begin{itemize}
   477   \item Plotting points
   533   \item Plot attributes and plotting points
   478   \item Plot attributes
       
   479   \item Lists
   534   \item Lists
   480   \item \kwrd{for}
   535   \item \kwrd{for}
   481   \item Reading files
   536   \item Reading files
   482   \item Tokenizing
   537   \item Tokenizing
   483   \item Strings
   538   \item Strings