summaryrefslogtreecommitdiff
path: root/day1/cheatsheet2.tex
blob: acffa20bb1fe13b45d3e66cf7e89fb9f1470a77c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
\documentclass[12pt]{article}


\title{Plotting Points}
\author{FOSSEE}
\usepackage{listings}
\lstset{language=Python,
    basicstyle=\ttfamily,
  commentstyle=\itshape\bfseries,
  showstringspaces=false,
}
\newcommand{\typ}[1]{\lstinline{#1}}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler}
\usepackage[scaled=.95]{helvet}

\begin{document}
\date{}
\vspace{-1in}
\begin{center}
\LARGE{Plotting Points}\\
\large{FOSSEE}
\end{center}

\section{Plotting Points with Lists}

\begin{lstlisting}
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 []: mtlist = []      # Empty List
In []: lst = [ 1, 2, 3, 4, 5] 
  \end{lstlisting}
Slicing
\begin{lstlisting}
In []: lst[1:3]         # A slice.
Out[]: [2, 3]

In []: lst[1:-1]
Out[]: [2, 3, 4]
\end{lstlisting}
\subsection{Appending to lists}
\begin{lstlisting}
In []: a = [ 6, 7, 8, 9]
In []: b = lst + a
In []: b
Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In []: lst.append(6)
In []: lst
Out[]: [ 1, 2, 3, 4, 5, 6]
\end{lstlisting}
\subsection{Iterating over a List}
\begin{lstlisting}
In []: for element in b:  # Iterating over the list, element-wise
 ....:     print element       # Print each element
 ....:
\end{lstlisting}

\section{Strings}
\subsection{Splitting Strings}
\begin{lstlisting}
In []: greet = ``hello world''
In []: print greet.split()
Out[]: ['hello', 'world']
In []: greet = ``hello, world''
In []: print greet.split(',')
Out[]: ['hello', ' world'] # Note the white space 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 white space 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}

\subsection{Plotting}
\begin{lstlisting}
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}