31 \end{lstlisting} |
32 \end{lstlisting} |
32 Exiting |
33 Exiting |
33 \begin{lstlisting} |
34 \begin{lstlisting} |
34 In [2]: (Ctrl-D)^D |
35 In [2]: (Ctrl-D)^D |
35 Do you really want to exit ([y]/n)? y |
36 Do you really want to exit ([y]/n)? y |
36 \end{lstlisting} |
37 \end{lstlisting} %$ |
37 |
38 |
38 \section{Plotting} |
39 \section{Plotting} |
|
40 \subsection{linspace} |
|
41 \typ{In []: x = linspace(start, stop, num)}\\ |
|
42 \typ{linspace} returns array of length \typ{num}, for which \typ{x[0] = start} and \typ{x[num-1] = stop} \\ |
|
43 \emph{Please note indices of array starts from zero(0)} |
39 |
44 |
|
45 \subsection{plot} |
|
46 \typ{In []: plot(X, Y)}\\ |
|
47 For given arrays of equal length(above case X and Y), \typ{plot} plots the corresponding *x* and *y* pairs taken from X and Y. |
|
48 |
|
49 \subsection{Colors of plots} |
|
50 \typ{In []: plot(y, sin(y), 'g')}\\ |
|
51 Plots graph with green color. Other options available are: |
|
52 \begin{lstlisting} |
|
53 'r' ---> Red |
|
54 'b' ---> Blue |
|
55 'r' ---> Red |
|
56 'c' ---> Cyan |
|
57 'm' ---> Magenta |
|
58 'y' ---> Yellow |
|
59 'k' ---> Black |
|
60 'w' ---> White |
|
61 \end{lstlisting} |
|
62 One can set the width of the plotline using optional argument \typ{linewidth}. For example:\\ |
|
63 \typ{In []: plot(x, cos(x), 'r', linewidth=2)}\\ |
|
64 Plots the line with linewidth = 2 |
|
65 \subsection{label and title} |
|
66 \typ{In []: xlabel('Length') #sets *x* axis label to Length}\\ |
|
67 \typ{In []: ylabel('Time') #sets *y* axis label to Time.}\\ |
|
68 \typ{In []: title('Sinusoid') #sets title of plot}\\ |
|
69 \\ |
|
70 \textbf{Additionally}\\ |
|
71 Pylab accepts TeX equation expressions in any text expression. To get something like:\\ |
|
72 $\sigma_i=15$ \\ |
|
73 on title of figure use: |
|
74 \begin{lstlisting} |
|
75 In []: title('$\sigma_i=15$') |
|
76 \end{lstlisting} |
|
77 Same way one can have TeX expression on xlabel, ylabel etc. |
|
78 |
|
79 \subsection{legends} |
|
80 \typ{In []: legend('sin(x)',loc=center)} \\ |
|
81 Place a legend on the current plot at location *loc*.\\ |
|
82 Apart from \typ{center}, some other \typ{loc} which can be specified are: |
|
83 \begin{lstlisting} |
|
84 'best' |
|
85 'right' |
|
86 'upper right' |
|
87 'upper left' |
|
88 'lower left' |
|
89 'lower right' |
|
90 'center left' |
|
91 'center right' |
|
92 'lower center' |
|
93 'upper center' |
|
94 \end{lstlisting} |
|
95 \newpage |
|
96 One can also mention explicit co-ordinates for placement of legend. |
|
97 \begin{lstlisting} |
|
98 In []: legend(['sin(2y)'], loc=(.8,.1)) |
|
99 \end{lstlisting} |
|
100 \typ{loc = 0, 1} (top left position of graph)\\ |
|
101 \typ{loc = 0.5, 0.5} (center of graph). |
|
102 |
|
103 \subsection{Annotate} |
|
104 \typ{In []: annotate('local max', xy=(1.5, 1))}\\ |
|
105 Annotates current plot with text, 'local max', at position specified to \typ{xy}. |
|
106 |
|
107 \subsection{Saving figures} |
|
108 \typ{In []: savefig('sinusoids.png')}\\ |
|
109 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. |
|
110 |
|
111 \subsection{Miscellaneous} |
|
112 \typ{In []: clf() #Clears the current plot area}\\ |
|
113 \typ{In []: close() #Closes the figure} |
|
114 \section{Saving and running scripts} |
|
115 \begin{itemize} |
|
116 \item \typ{\%hist}\\ |
|
117 It returns the logs of all commands(including mistakes) used in IPython interpreter. |
|
118 \item \typ{\%hist -n}\\ |
|
119 It disables the line number representation of logs. |
|
120 \item \typ{\%save four\_plot.py 16 18-27}\\ |
|
121 For creating a script named four\_plot which includes line 16 and line 18 to 27 of logs. |
|
122 \item \typ{\%run -i four\_plot.py}\\ |
|
123 Running the python script inside IPython interpreter. |
|
124 \end{itemize} |
|
125 |
|
126 \section{Example} |
40 \begin{lstlisting} |
127 \begin{lstlisting} |
41 In [1]: x = linspace(0, 2*pi, 50) |
128 In []: x = linspace(0, 2*pi, 50) |
42 In [2]: plot(x, sin(x)) |
129 In []: plot(x, sin(x), 'g') |
43 In [3]: xlabel('x') |
130 In []: plot(x, cos(x), 'r', linewidth=2) |
44 In [4]: ylabel('sin(x)') |
131 In []: xlabel('x') |
45 In [5]: title('Sinusoids') |
132 In []: title('Sinusoidal Waves') |
46 In [6]: legend(['sin(y)']) |
133 In []: legend(['sin(x)', 'cos(x)']) |
47 In [7]: legend(['sin(2y)'], loc = 'center') |
134 In []: annotate('origin', xy=(0, 0)) |
48 # loc = 'upper right', 'upper left', 'lower left, 'lower right', 'center left', |
135 In []: xmin, xman = xlim() # returns current X axis limits. |
49 # 'center right', 'lower center', 'upper center', 'best', 'right', 'center' |
136 In []: ymin, ymax = ylim() |
|
137 In []: xlim(0, 2 * pi) # sets the X axis limits to passed values |
|
138 In []: ylim(ymin - 0.2, ymax + 0.2) |
50 |
139 |
51 In [8]: legend(['sin(2y)'], loc = (.8, .1)) |
140 In []: savefig('sin.png') # Save figure |
52 |
141 In []: close() |
53 In [9]: savefig('sin.png') # Save figure |
|
54 In [10]: close() # Closes the figure |
|
55 |
|
56 In [11]: clf() # Clears the Plot area |
|
57 |
|
58 In [12]: plot(y, sin(y), 'g') |
|
59 # Colors can be: 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w' |
|
60 |
|
61 In [13]: plot(y, cos(y), 'r', linewidth=2) |
|
62 |
|
63 In [14]: legend(['x', '-x']) |
|
64 In [15]: annotate('origin', xy=(0, 0)) |
|
65 |
|
66 In [16]: xmin, xman = xlim() # Without arguments gets |
|
67 In [17]: ymin, ymax = ylim() # values |
|
68 |
|
69 In [18]: xlim(0, 2 * pi) # With values, sets the |
|
70 In [19]: ylim(ymin - 0.2, ymax + 0.2) # specified values |
|
71 \end{lstlisting} |
142 \end{lstlisting} |
72 |
143 |
73 \section{Saving and running scripts} |
144 \section{References} |
74 \begin{itemize} |
145 \begin{itemize} |
75 \item \typ{\%hist} |
146 \item For documentation on IPython refer: \\ http://ipython.scipy.org/moin/Documentation |
76 \item \typ{\%save four\_plot.py 16 18-27} |
147 \item Plotting(matplotlib) related documentation are available at:\\ http://matplotlib.sourceforge.net/contents.html |
77 \item \typ{\%run -i four\_plot.py} |
148 \item Explore examples and plots based on matplotlib at \\ http://matplotlib.sourceforge.net/examples/index.html |
78 \end{itemize} |
149 \end{itemize} |
79 |
|
80 \end{document} |
150 \end{document} |
81 |
151 |