day1/cheatsheet2.tex
changeset 313 f5bcb974a665
parent 311 5303c34a3180
child 314 c9f05808e1c4
equal deleted inserted replaced
312:694b7bdeef09 313:f5bcb974a665
    32 In []: x = [0, 1, 2, 3]
    32 In []: x = [0, 1, 2, 3]
    33 In []: y = [7, 11, 15, 19]
    33 In []: y = [7, 11, 15, 19]
    34 In []: plot(x, y)
    34 In []: plot(x, y)
    35 In []: clf()
    35 In []: clf()
    36 In []: plot(x, y, 'o')  # Plotting Circles
    36 In []: plot(x, y, 'o')  # Plotting Circles
    37 #Dots - '.', #Dashed lines - '--' #Lines - '-'
       
    38 \end{lstlisting}
    37 \end{lstlisting}
    39 
    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.
    40 \section{Lists}
    81 \section{Lists}
    41 
    82 
    42 Initializing
    83 Initializing
    43   \begin{lstlisting}
    84   \begin{lstlisting}
    44 In []: mtlist = []      # Empty List
    85 In []: mtlist = []      # Empty List
    50 Out[]: [2, 3]
    91 Out[]: [2, 3]
    51 
    92 
    52 In []: lst[1:-1]
    93 In []: lst[1:-1]
    53 Out[]: [2, 3, 4]
    94 Out[]: [2, 3, 4]
    54 \end{lstlisting}
    95 \end{lstlisting}
    55 Appending to lists
    96 \subsection{Appending to lists}
    56 \begin{lstlisting}
    97 \begin{lstlisting}
    57 In []: a = [ 6, 7, 8, 9]
    98 In []: a = [ 6, 7, 8, 9]
    58 In []: b = lst + a
    99 In []: b = lst + a
    59 In []: b
   100 In []: b
    60 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
   101 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    61 
   102 
    62 In []: lst.append(6)
   103 In []: lst.append(6)
    63 In []: lst
   104 In []: lst
    64 Out[]: [ 1, 2, 3, 4, 5, 6]
   105 Out[]: [ 1, 2, 3, 4, 5, 6]
    65 \end{lstlisting}
   106 \end{lstlisting}
    66 Iterating over a List
   107 \subsection{Iterating over a List}
    67 \begin{lstlisting}
   108 \begin{lstlisting}
    68 In []: for each in b:  # Iterating over the list, element-wise
   109 In []: for element in b:  # Iterating over the list, element-wise
    69  ....:     print b       # Print each element
   110  ....:     print element       # Print each element
    70  ....:
   111  ....:
    71 \end{lstlisting}
   112 \end{lstlisting}
    72 
   113 
    73 Splitting Strings
   114 \section{Strings}
       
   115 \subsection{Splitting Strings}
    74 \begin{lstlisting}
   116 \begin{lstlisting}
    75 In [20]: line = '1.2000e-01 7.4252e-01'
   117 In []: greet = ``hello world''
    76 In [21]: point = line.split()    # Splits the string at the space
   118 In []: print greet.split()
    77 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'
    78 \end{lstlisting}
   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.
    79 
   127 
    80 Plotting from Files
   128 \section{Plotting from Files}
       
   129 \subsection{Opening, reading and writing files}
       
   130 
    81 \begin{lstlisting}
   131 \begin{lstlisting}
    82 In [22]: L = []
   132   In []: f = open('datafile.txt') #By default opens in read mode. If file does not exist then it throws an exception
    83 In [23]: T = []
   133   In []: f = open('datafile.txt','r') #Specifying the read mode
    84 
   134   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.
    85 #Open a file & operate on each line
   135 \end{lstlisting}
    86 In [24]: for line in open('pendulum.txt'):  
   136 \subsection{Plotting}
    87   ....     point = line.split()
   137 \begin{lstlisting}
    88   ....     L.append(float(point[0]))
   138 l = []
    89   ....     T.append(float(point[1]))
   139 t = []
    90 In [25]: TSq = []
   140 for line in open('pendulum.txt'):
    91 In [26]: for t in T:
   141     point = line.split()
    92  ....:     TSq.append(t*t)
   142     l.append(float(point[0]))
    93  ....:     
   143     t.append(float(point[1]))
    94  ....:     
   144 tsq = []
    95 In [27]: plot(L, TSq, '.')
   145 for time in t:
       
   146     tsq.append(time*time)
       
   147 plot(l, tsq, '.')
    96 \end{lstlisting}
   148 \end{lstlisting}
    97 
   149 
    98 \end{document}
   150 \end{document}
    99 
   151