day1/cheatsheet2.tex
changeset 334 2214b5dba4d4
parent 330 46533051b9d3
child 341 7ae88b9da553
equal deleted inserted replaced
333:25b18b51be41 334:2214b5dba4d4
    23 \vspace{-1in}
    23 \vspace{-1in}
    24 \begin{center}
    24 \begin{center}
    25 \LARGE{Plotting Points}\\
    25 \LARGE{Plotting Points}\\
    26 \large{FOSSEE}
    26 \large{FOSSEE}
    27 \end{center}
    27 \end{center}
    28 \section{Plotting from Data files}
    28 
    29 \begin{verbatim}
       
    30 l = [] #Empty List
       
    31 t = []
       
    32 for line in open('pendulum.txt'): # Opening & Reading files
       
    33     points = line.split() # Splitting a string
       
    34     l.append(float(points[0])) # Appending to a list
       
    35     t.append(float(points[1]))
       
    36 tsq = []
       
    37 for time in t:  #Iterating through lists
       
    38     tsq.append(t*t)
       
    39 plot(l, tsq, '.') # Plotting points
       
    40 \end{verbatim}
       
    41 \section{Plotting Points with Lists}
    29 \section{Plotting Points with Lists}
    42 
    30 
    43 \begin{lstlisting}
    31 \begin{lstlisting}
    44 In [1]: x = [0, 1, 2, 3]
    32 In []: x = [0, 1, 2, 3] # Creating a list
    45 In [2]: y = [7, 11, 15, 19]
    33 In []: y = [7, 11, 15, 19]
    46 In [3]: plot(x, y)
    34 In []: plot(x, y)
    47 In [4]: clf()
    35 In []: clf()
    48 In [5]: plot(x, y, 'o')  # Plotting Circles
    36 In []: plot(x, y, 'o')  # Plotting Circles
    49 #Dots - '.', #Dashed lines - '--' #Lines - '-'
       
    50 \end{lstlisting}
    37 \end{lstlisting}
    51 
    38 
       
    39 \subsection{Line style/marker}
       
    40 \begin{lstlisting}
       
    41 The following format string characters are accepted 
       
    42 to control the line style or marker:
       
    43     
       
    44     ================    ===============================
       
    45     character           description
       
    46     ================    ===============================
       
    47     '-'                 solid line style
       
    48     '--'                dashed line style
       
    49     '-.'                dash-dot line style
       
    50     ':'                 dotted line style
       
    51     '.'                 point marker
       
    52     ','                 pixel marker
       
    53     'o'                 circle marker
       
    54     'v'                 triangle_down marker
       
    55     '^'                 triangle_up marker
       
    56     '<'                 triangle_left marker
       
    57     '>'                 triangle_right marker
       
    58     '1'                 tri_down marker
       
    59     '2'                 tri_up marker
       
    60     '3'                 tri_left marker
       
    61     '4'                 tri_right marker
       
    62     's'                 square marker
       
    63     'p'                 pentagon marker
       
    64     '*'                 star marker
       
    65     'h'                 hexagon1 marker
       
    66     'H'                 hexagon2 marker
       
    67     '+'                 plus marker
       
    68     'x'                 x marker
       
    69     'D'                 diamond marker
       
    70     'd'                 thin_diamond marker
       
    71     '|'                 vline marker
       
    72     '_'                 hline marker
       
    73     ================    ===============================
       
    74 
       
    75 \end{lstlisting}
       
    76 
       
    77 \subsection{Marker combinations}
       
    78 \typ{In []: plot(x, y, 'ro')} \\
       
    79 This plots figure with red colored filled circles.\\
       
    80 Similarly other combination of colors and marker can be used.
    52 \section{Lists}
    81 \section{Lists}
    53 
    82 
    54 Initializing
    83 Initializing
    55   \begin{lstlisting}
    84   \begin{lstlisting}
    56 In [10]: mtlist = []      # Empty List
    85 In []: mtlist = []      # Empty List
    57 In [11]: lst = [ 1, 2, 3, 4, 5] 
    86 In []: lst = [ 1, 2, 3, 4, 5] 
    58   \end{lstlisting}
    87   \end{lstlisting}
    59 Slicing
    88 Slicing
    60 \begin{lstlisting}
    89 \begin{lstlisting}
    61 In [12]: lst[1:3]         # A slice.
    90 In []: lst[1:3]         # A slice.
    62 Out[12]: [2, 3]
    91 Out[]: [2, 3]
    63 
    92 
    64 In [13]: lst[1:-1]
    93 In []: lst[1:-1]
    65 Out[13]: [2, 3, 4]
    94 Out[]: [2, 3, 4]
    66 \end{lstlisting}
    95 \end{lstlisting}
    67 Appending to lists
    96 \subsection{Appending to lists}
    68 \begin{lstlisting}
    97 \begin{lstlisting}
    69 In [14]: a = [ 6, 7, 8, 9]
    98 In []: a = [ 6, 7, 8, 9]
    70 In [15]: b = lst + a
    99 In []: b = lst + a
    71 In [16]: b
   100 In []: b
    72 Out[16]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
   101 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    73 
   102 
    74 In [17]: lst.append(6)
   103 In []: lst.append(6)
    75 In [18]: lst
   104 In []: lst
    76 Out[18]: [ 1, 2, 3, 4, 5, 6]
   105 Out[]: [ 1, 2, 3, 4, 5, 6]
    77 \end{lstlisting}
   106 \end{lstlisting}
    78 
   107 \subsection{Iterating over a List}
    79 Iterating over a List
       
    80 \begin{lstlisting}
   108 \begin{lstlisting}
    81 In [19]: for each in b:  # Iterating over the list, element-wise
   109 In []: for element in b:  # Iterating over the list, element-wise
    82  ....:     print b       # Print each element
   110  ....:     print element       # Print each element
    83  ....:
   111  ....:
    84 \end{lstlisting}
   112 \end{lstlisting}
    85 
   113 
    86 Splitting Strings
   114 \section{Strings}
       
   115 \subsection{Splitting Strings}
    87 \begin{lstlisting}
   116 \begin{lstlisting}
    88 In [20]: line = '1.2000e-01 7.4252e-01'
   117 In []: greet = ``hello world''
    89 In [21]: point = line.split()    # Splits the string at the space
   118 In []: print greet.split()
    90 Out[21]: ['1.2000e-01', '7.4252e-01']
   119 Out[]: ['hello', 'world']
       
   120 In []: greet = ``hello, world''
       
   121 In []: print greet.split(',')
       
   122 Out[]: ['hello', ' world'] # Note the whitespace before 'world'
       
   123 \end{lstlisting}
       
   124 A string can be split based on the delimiter specified within quotes. A combination of more than one delimiter can also be used.\\
       
   125 \typ{In []: greet.split(', ')}\\
       
   126 \typ{Out[]: ['hello', 'world']}\\Note the whitespace is not there anymore.
       
   127 \newpage
       
   128 \section{Plotting from Files}
       
   129 \subsection{Opening files}
       
   130 
       
   131 \typ{In []: f = open('datafile.txt')}\\By default opens in read mode. \\If file does not exist then it throws an exception\\
       
   132 \typ{In []: f = open('datafile.txt','r')}\\Specifying the read mode\\
       
   133 \typ{In []: f = open('datafile.txt', 'w')}\\Opens the file in write mode. \\If the file already exists, then it deletes all the previous content and opens.
       
   134 
       
   135 \subsection{Reading from files}
       
   136 Just like lists files are iterable as well.
       
   137 
       
   138 \begin{lstlisting}
       
   139   In []: for line in f:
       
   140     ...:     print line
       
   141     ...: 
       
   142     ...:
    91 \end{lstlisting}
   143 \end{lstlisting}
    92 
   144 
    93 Plotting from Files
   145 \subsection{Plotting}
    94 \begin{lstlisting}
   146 \begin{lstlisting}
    95 In [22]: L = []
   147 l = []
    96 In [23]: T = []
   148 t = []
    97 
   149 for line in open('pendulum.txt'):
    98 #Open a file & operate on each line
   150     point = line.split()
    99 In [24]: for line in open('pendulum.txt'):  
   151     l.append(float(point[0]))
   100   ....     point = line.split()
   152     t.append(float(point[1]))
   101   ....     L.append(float(point[0]))
   153 tsq = []
   102   ....     T.append(float(point[1]))
   154 for time in t:
   103 In [25]: TSq = []
   155     tsq.append(time*time)
   104 In [26]: for t in T:
   156 plot(l, tsq, '.')
   105  ....:     TSq.append(t*t)
       
   106  ....:     
       
   107  ....:     
       
   108 In [27]: plot(L, TSq, '.')
       
   109 \end{lstlisting}
   157 \end{lstlisting}
   110 
   158 
   111 \end{document}
   159 \end{document}
   112 
   160