\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}