author | bhanu |
Mon, 15 Nov 2010 15:07:22 +0530 | |
changeset 505 | 394c3642cf9c |
parent 467 | 501383b753c1 |
permissions | -rw-r--r-- |
362 | 1 |
.. Objectives |
2 |
.. ---------- |
|
3 |
||
4 |
.. By the end of this tutorial you will -- |
|
5 |
||
6 |
.. 1. Create simple plots of mathematical functions |
|
7 |
.. #. Use the Figure window to study plots better |
|
8 |
||
9 |
||
10 |
||
11 |
.. Prerequisites |
|
12 |
.. ------------- |
|
13 |
||
14 |
.. Installation of required tools |
|
15 |
.. Ipython |
|
16 |
||
17 |
.. Author : Amit Sethi |
|
18 |
Internal Reviewer : |
|
19 |
External Reviewer : |
|
20 |
Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
21 |
||
22 |
Script |
|
23 |
------- |
|
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
24 |
{{{ Show the Title Slide }}} |
362 | 25 |
|
321 | 26 |
Hello and welcome to the tutorial on creating simple plots using |
27 |
Python.This tutorial is presented by the Fossee group. |
|
28 |
||
29 |
I hope you have IPython running on your computer. |
|
30 |
||
31 |
In this tutorial we will look at plot command and also how to study |
|
32 |
the plot using the UI. |
|
33 |
||
34 |
{{{ Show Outline Slide }}} |
|
35 |
||
36 |
Lets start ipython on your shell, type :: |
|
37 |
||
38 |
$ipython -pylab |
|
39 |
||
40 |
||
41 |
Pylab is a python library which provides plotting functionality.It |
|
42 |
also provides many other important mathematical and scientific |
|
43 |
functions. After running IPython -pylab in your shell if at the top of |
|
44 |
the result of this command, you see something like :: |
|
45 |
||
46 |
||
47 |
`ERROR: matplotlib could NOT be imported! Starting normal |
|
48 |
IPython.` |
|
49 |
||
50 |
||
51 |
{{{ Slide with Error written on it }}} |
|
52 |
||
342 | 53 |
|
321 | 54 |
Then you have to install matplotlib and run this command again. |
55 |
||
56 |
Now type in your ipython shell :: |
|
57 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
58 |
linpace? |
321 | 59 |
|
60 |
||
61 |
||
62 |
as the documentation says, it returns `num` evenly spaced samples, |
|
63 |
calculated over the interval start and stop. To illustrate this, lets |
|
64 |
do it form 1 to 100 and try 100 points. :: |
|
65 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
66 |
linspace(1,100,100) |
321 | 67 |
|
68 |
As you can see a sequence of numbers from 1 to 100 appears. |
|
69 |
||
70 |
Now lets try 200 points between 0 and 1 you do this by typing :: |
|
71 |
||
72 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
73 |
linspace(0,1,200) |
321 | 74 |
|
75 |
0 for start , 1 for stop and 200 for no of points. In linspace |
|
76 |
the start and stop points can be integers, decimals , or |
|
77 |
constants. Let's try and get 100 points between -pi to pi. Type :: |
|
78 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
79 |
p = linspace(-pi,pi,100) |
321 | 80 |
|
81 |
||
82 |
'pi' here is constant defined by pylab. Save this to the variable, p |
|
83 |
. |
|
84 |
||
85 |
If you now :: |
|
86 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
87 |
len(p) |
321 | 88 |
|
89 |
You will get the no. of points. len function gives the no of elements |
|
90 |
of a sequence. |
|
91 |
||
92 |
||
93 |
Let's try and plot a cosine curve between -pi and pi using these |
|
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
94 |
points. Simply type:: |
321 | 95 |
|
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
96 |
plot(p,cos(points)) |
321 | 97 |
|
98 |
||
99 |
Here cos(points) gets the cosine value at every corresponding point to |
|
100 |
p. |
|
101 |
||
102 |
||
103 |
We can also save cos(points) to variable cosine and plot it using |
|
104 |
plot.:: |
|
105 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
106 |
cosine=cos(points) |
321 | 107 |
|
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
108 |
plot(p,cosine) |
321 | 109 |
|
110 |
||
111 |
||
112 |
Now do :: |
|
113 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
114 |
clf() |
321 | 115 |
|
116 |
this will clear the plot. |
|
117 |
||
118 |
This is done because any other plot we try to make shall come on the |
|
119 |
same drawing area. As we do not wish to clutter the area with |
|
120 |
overlaid plots , we just clear it with clf(). Now lets try a sine |
|
121 |
plot. :: |
|
122 |
||
123 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
124 |
plot(p,sin(p)) |
321 | 125 |
|
126 |
||
127 |
||
128 |
||
129 |
The Window on which the plot appears can be used to study it better. |
|
130 |
||
342 | 131 |
{{{ Show the slide with all the buttons on it }}} |
132 |
||
321 | 133 |
First of all moving the mouse around gives us the point where mouse |
134 |
points at. |
|
135 |
||
136 |
Also we have some buttons the right most among them is |
|
137 |
for saving the file. |
|
138 |
||
139 |
Just click on it specifying the name of the file. We will save the plot |
|
140 |
by the name sin_curve in pdf format. |
|
141 |
||
142 |
||
143 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
144 |
{{{ Show how to save the file }}} |
321 | 145 |
|
146 |
As you can see I can specify format of file from the dropdown. |
|
147 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
148 |
Formats like png ,eps ,pdf, ps are available. |
321 | 149 |
|
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
150 |
Left to the save button is the slider button to specify the margins. |
321 | 151 |
|
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
152 |
{{{ Show how to zoom. Press zoom button and specify region to zoom }}} |
321 | 153 |
|
154 |
Left to this is zoom button to zoom into the plot. Just specify the |
|
155 |
region to zoom into. |
|
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
156 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
157 |
{{{ Press Move button and move the axes. }}} |
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
158 |
|
321 | 159 |
The button left to it can be used to move the axes of the plot. |
160 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
161 |
{{{ Press Back and Forward Button }}} |
321 | 162 |
|
163 |
The next two buttons with a left and right arrow icons change the state of the |
|
164 |
plot and take it to the previous state it was in. It more or less acts like a |
|
165 |
back and forward button in the browser. |
|
166 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
167 |
{{{ Press home button }}} |
321 | 168 |
|
169 |
The last one is 'home' referring to the initial plot. |
|
170 |
||
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
171 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
172 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
173 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
174 |
Following is an exercise that you must do. |
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
175 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
176 |
%% %% Plot (sin(x)*sin(x))/x . |
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
177 |
1. Save the plot by the sinsquarebyx.pdf in pdf format. |
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
178 |
2. Zoom and find the maxima. |
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
179 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
180 |
3. Bring it back to initial position. |
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
181 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
182 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
183 |
Please, pause the video here. Do the exercise and then continue. |
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
184 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
185 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
186 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
187 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
188 |
|
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
189 |
|
321 | 190 |
|
191 |
||
192 |
||
193 |
{{{ Summary Slide }}} |
|
194 |
||
195 |
In this tutorial we have looked at |
|
196 |
||
197 |
1. Starting Ipython with pylab |
|
198 |
||
199 |
2. Using linspace function to create `num` equaly spaced points in a region. |
|
200 |
||
201 |
3. Finding length of sequnces using len. |
|
202 |
||
203 |
4. Plotting mathematical functions using plot. |
|
204 |
||
205 |
4. Clearing drawing area using clf |
|
206 |
||
342 | 207 |
5. Using the UI of plot for studying it better . Using functionalities like save , zoom and moving the plots on x and y axis |
321 | 208 |
|
342 | 209 |
|
467
501383b753c1
Exercises and Slides added to Using plot interactively
Amit Sethi
parents:
409
diff
changeset
|
210 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
321 | 211 |
|
212 |
||
213 |
||
214 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
215 |
||
216 |
||
217 |
||
218 |
Hope you have enjoyed and found it useful. |
|
219 |
||
220 |
Thankyou |
|
221 |
||
222 |
||
223 |
||
224 |
Author : Amit Sethi |
|
225 |
Internal Reviewer : |
|
226 |
Internal Reviewer 2 : |