diff -r 25b18b51be41 -r 2214b5dba4d4 day1/cheatsheet2.tex --- a/day1/cheatsheet2.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/cheatsheet2.tex Tue Dec 29 19:25:11 2009 +0530 @@ -25,87 +25,135 @@ \LARGE{Plotting Points}\\ \large{FOSSEE} \end{center} -\section{Plotting from Data files} -\begin{verbatim} -l = [] #Empty List -t = [] -for line in open('pendulum.txt'): # Opening & Reading files - points = line.split() # Splitting a string - l.append(float(points[0])) # Appending to a list - t.append(float(points[1])) -tsq = [] -for time in t: #Iterating through lists - tsq.append(t*t) -plot(l, tsq, '.') # Plotting points -\end{verbatim} + \section{Plotting Points with Lists} \begin{lstlisting} -In [1]: x = [0, 1, 2, 3] -In [2]: y = [7, 11, 15, 19] -In [3]: plot(x, y) -In [4]: clf() -In [5]: plot(x, y, 'o') # Plotting Circles -#Dots - '.', #Dashed lines - '--' #Lines - '-' +In []: x = [0, 1, 2, 3] # Creating a list +In []: y = [7, 11, 15, 19] +In []: plot(x, y) +In []: clf() +In []: plot(x, y, 'o') # Plotting Circles \end{lstlisting} +\subsection{Line style/marker} +\begin{lstlisting} +The following format string characters are accepted +to control the line style or marker: + + ================ =============================== + character description + ================ =============================== + '-' solid line style + '--' dashed line style + '-.' dash-dot line style + ':' dotted line style + '.' point marker + ',' pixel marker + 'o' circle marker + 'v' triangle_down marker + '^' triangle_up marker + '<' triangle_left marker + '>' triangle_right marker + '1' tri_down marker + '2' tri_up marker + '3' tri_left marker + '4' tri_right marker + 's' square marker + 'p' pentagon marker + '*' star marker + 'h' hexagon1 marker + 'H' hexagon2 marker + '+' plus marker + 'x' x marker + 'D' diamond marker + 'd' thin_diamond marker + '|' vline marker + '_' hline marker + ================ =============================== + +\end{lstlisting} + +\subsection{Marker combinations} +\typ{In []: plot(x, y, 'ro')} \\ +This plots figure with red colored filled circles.\\ +Similarly other combination of colors and marker can be used. \section{Lists} Initializing \begin{lstlisting} -In [10]: mtlist = [] # Empty List -In [11]: lst = [ 1, 2, 3, 4, 5] +In []: mtlist = [] # Empty List +In []: lst = [ 1, 2, 3, 4, 5] \end{lstlisting} Slicing \begin{lstlisting} -In [12]: lst[1:3] # A slice. -Out[12]: [2, 3] +In []: lst[1:3] # A slice. +Out[]: [2, 3] -In [13]: lst[1:-1] -Out[13]: [2, 3, 4] +In []: lst[1:-1] +Out[]: [2, 3, 4] \end{lstlisting} -Appending to lists +\subsection{Appending to lists} \begin{lstlisting} -In [14]: a = [ 6, 7, 8, 9] -In [15]: b = lst + a -In [16]: b -Out[16]: [1, 2, 3, 4, 5, 6, 7, 8, 9] +In []: a = [ 6, 7, 8, 9] +In []: b = lst + a +In []: b +Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9] -In [17]: lst.append(6) -In [18]: lst -Out[18]: [ 1, 2, 3, 4, 5, 6] +In []: lst.append(6) +In []: lst +Out[]: [ 1, 2, 3, 4, 5, 6] \end{lstlisting} - -Iterating over a List +\subsection{Iterating over a List} \begin{lstlisting} -In [19]: for each in b: # Iterating over the list, element-wise - ....: print b # Print each element +In []: for element in b: # Iterating over the list, element-wise + ....: print element # Print each element ....: \end{lstlisting} -Splitting Strings +\section{Strings} +\subsection{Splitting Strings} \begin{lstlisting} -In [20]: line = '1.2000e-01 7.4252e-01' -In [21]: point = line.split() # Splits the string at the space -Out[21]: ['1.2000e-01', '7.4252e-01'] +In []: greet = ``hello world'' +In []: print greet.split() +Out[]: ['hello', 'world'] +In []: greet = ``hello, world'' +In []: print greet.split(',') +Out[]: ['hello', ' world'] # Note the whitespace before 'world' +\end{lstlisting} +A string can be split based on the delimiter specified within quotes. A combination of more than one delimiter can also be used.\\ +\typ{In []: greet.split(', ')}\\ +\typ{Out[]: ['hello', 'world']}\\Note the whitespace is not there anymore. +\newpage +\section{Plotting from Files} +\subsection{Opening files} + +\typ{In []: f = open('datafile.txt')}\\By default opens in read mode. \\If file does not exist then it throws an exception\\ +\typ{In []: f = open('datafile.txt','r')}\\Specifying the read mode\\ +\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. + +\subsection{Reading from files} +Just like lists files are iterable as well. + +\begin{lstlisting} + In []: for line in f: + ...: print line + ...: + ...: \end{lstlisting} -Plotting from Files +\subsection{Plotting} \begin{lstlisting} -In [22]: L = [] -In [23]: T = [] - -#Open a file & operate on each line -In [24]: for line in open('pendulum.txt'): - .... point = line.split() - .... L.append(float(point[0])) - .... T.append(float(point[1])) -In [25]: TSq = [] -In [26]: for t in T: - ....: TSq.append(t*t) - ....: - ....: -In [27]: plot(L, TSq, '.') +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, '.') \end{lstlisting} \end{document}