summaryrefslogtreecommitdiff
path: root/day1/cheatsheet1.tex
blob: 12edd5ef6bc54794ae394bfb19e54020805587b1 (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
\documentclass[12pt]{article}


\title{Interactive Plotting}
\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{Interactive Plotting}\\
\large{FOSSEE}
\end{center}
\section{Starting up...}

\begin{lstlisting}
  $ ipython -pylab  
\end{lstlisting}
Exiting 
\begin{lstlisting}     
In [2]: (Ctrl-D)^D
Do you really want to exit ([y]/n)? y
\end{lstlisting} %$

\section{Plotting}
\subsection{linspace}
\typ{In []: x = linspace(start, stop, num)}\\
\typ{linspace} returns array of length \typ{num}, for which \typ{x[0] = start} and \typ{x[num-1] = stop} \\
\emph{Please note indices of array starts from zero(0)}

\subsection{plot}
\typ{In []: plot(X, Y)}\\
For given arrays of equal length(above case X and Y), \typ{plot} plots the correspoding *x* and *y* pairs taken from X and Y.

\subsection{Colors of plots}
\typ{In []: plot(y, sin(y), 'g')}\\
Plots graph with green color. Other options available are:
\begin{lstlisting}
  'r' ---> Red
  'b' ---> Blue
  'r' ---> Red 
  'c' ---> Cyan 
  'm' ---> Magenta
  'y' ---> Yellow
  'k' ---> Black 
  'w' ---> White
\end{lstlisting}
One can set the width of the plotline using optional argument \typ{linewidth}. For example:\\
\typ{In []: plot(x, cos(x), 'r', linewidth=2)}\\
Plots the line with linewidth = 2
\subsection{label and title}
\typ{In []: xlabel('Length') #sets *x* axis label to Length}\\ 
\typ{In []: ylabel('Time') #sets *y* axis label to Time.}\\
\typ{In []: title('Sinusoids') #sets title of plot}\\
\\
\textbf{Additionally}\\
Pylab accepts TeX equation expressions in any text expression. To get something like:\\
$\sigma_i=15$ \\
on title of figure use: 
\begin{lstlisting}
In []: title('$\sigma_i=15$')
\end{lstlisting}  
Same way one can have TeX expression on xlabel, ylabel etc.

\subsection{legends}
\typ{In []: legend('sin(x)',loc=center)} \\
Placec a legend on the current plot at location *loc*.\\
Apart from \kwrd{center}, some other \kwrd{loc} which can be specified are:
\begin{lstlisting}
'best'
'right'
'upper right'
'upper left'      
'lower left'      
'lower right'     
'center left'     
'center right'    
'lower center'    
'upper center'    
\end{lstlisting}
\newpage
One can also mention explicit co-ordinates for placement of legend. 
\begin{lstlisting}
In []: legend(['sin(2y)'], loc=(.8,.1)) 
\end{lstlisting}
\typ{loc = 0, 1} (top left position of graph)\\
\typ{loc = 0.5, 0.5} (center of graph).

\subsection{Annotate}
\typ{In []: annotate('local max', xy=(1.5, 1))}\\
Annotates current plot with text, 'local max', at position specified to \typ{xy}.

\subsection{Saving figures}
\typ{In []: savefig('sinusoids.png')}\\
Saves the current figure with file name 'sinusoids.png' in current working directory. One can save figure in any of these formats: png, pdf, ps, eps and svg.

\subsection{Miscellaneous}
\typ{In []: clf() #Clears the current plot area}\\
\typ{In []: close() #Closes the figure}
\section{Saving and running scripts}
\begin{itemize}
  \item \typ{\%hist}\\
  It returns the logs of all commands(including mistakes) used in IPython interpreter.
  \item \typ{\%hist -n}\\
It disables the line number representation of logs.
  \item \typ{\%save four\_plot.py 16 18-27}\\
For creating a script named four\_plot which includes line 16 and line 18 to 27 of logs.
  \item \typ{\%run -i four\_plot.py}\\
Running the python script inside IPython interpreter.
\end{itemize}

\section{Example}
  \begin{lstlisting}
In []: x = linspace(0, 2*pi, 50)
In []: plot(x, sin(x), 'g')
In []: plot(x, cos(x), 'r', linewidth=2)
In []: xlabel('x')
In []: title('Sinusoidal Waves')
In []: legend(['sin(x)', 'cos(x)'])
In []: annotate('origin', xy=(0, 0))
In []: xmin, xman = xlim()  # returns current X axis limits.
In []: ymin, ymax = ylim()
In []: xlim(0, 2 * pi) # sets the X axis limits to passed values
In []: ylim(ymin - 0.2, ymax + 0.2) 

In []: savefig('sin.png')   # Save figure
In []: close()              
  \end{lstlisting}

\section{References}
\begin{itemize}
  \item For documentation on IPython refer: \\ \url{http://ipython.scipy.org/moin/Documentation}
  \item Plotting(matplotlib) related documentation are available at:\\ \url{http://matplotlib.sourceforge.net/contents.html}
  \item Explore examples and plots based on matplotlib at \\ \url{http://matplotlib.sourceforge.net/examples/index.html}
\end{itemize}
\end{document}