27 |
27 |
28 type again : |
28 type again : |
29 $ ipython -pylab |
29 $ ipython -pylab |
30 press RETURN |
30 press RETURN |
31 |
31 |
32 First, we will create a sequence of equally spaced points starting from 0 to 2*pi , we will use function linspace for that |
32 In order to plot, we need a set of points. Let us create a sequence of equally spaced points starting from 0 to 2*pi. For this we use linspace |
33 |
33 |
34 Type: |
34 Type: |
35 In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word |
35 In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word |
36 |
36 |
37 In [] x=linspace( RETURN |
37 In [] x=linspace( RETURN |
38 oops I made a mistake . As you can see I made the mistake of not writing command correctly |
38 Ouch! I made a mistake . As you can see I made the mistake of not writing command correctly |
39 and Ipython changed the prompt . To get the old prompt back type crtl-c |
39 and Ipython changed the prompt . To get the old prompt back press crtl-c |
40 |
40 |
41 In []: x = linspace(0, 2*pi, 100) |
41 In []: x = linspace(0, 2*pi, 100) |
42 |
42 |
43 *As you can see linspace can take three parameters start, stop, and num and returns num evenly space points . You can scroll through the help to know more about the function |
43 *To know more about any function, in this case say, for 'linspace' you can type ? after it . |
44 |
|
45 |
|
46 *To know more about any function, example for the 'linspace' function you can type ? after it . |
|
47 |
44 |
48 In []: linspace? |
45 In []: linspace? |
49 |
46 |
50 It shows documentation related to linspace function. 'help' talks in detail about arguments to be passed, return values, some examples on usage. You can scroll the help using up, and down arrows keys. |
47 It shows documentation related to linspace function. 'help' gives details about arguments to be passed, return values and also some examples on usage. You can scroll the help using up, and down arrows keys. |
51 At any time you want to come out of the help use 'q' key. |
48 To exit from the help menu type 'q'. |
52 See how easy it is to get help in IPython. |
49 |
|
50 *As you can see linspace can take three parameters the starting point, the ending point and the number of points. |
|
51 |
|
52 Let us see what x contains now. Type 'x' and hit enter. |
|
53 |
|
54 We see that x is a sequence of numbers from 0 to 2*pi. We can check the length of x by typing |
|
55 |
|
56 In []: len(x) |
53 |
57 |
54 To obtain the plot we use, |
58 To obtain the plot we use, |
55 In []: plot(x, sin(x)) |
59 In []: plot(x, sin(x)) |
56 *** |
60 *** |
57 As you can see a plot has appeared on the screen. |
61 As you can see a plot has appeared on the screen. |
72 Hmm we also got the axis's nicely labeled and the plot titled but there is still a important detail left.That might leave a teacher unsatisfied, it lacks a legend. |
76 Hmm we also got the axis's nicely labeled and the plot titled but there is still a important detail left.That might leave a teacher unsatisfied, it lacks a legend. |
73 |
77 |
74 Add a legend to the plot by typing |
78 Add a legend to the plot by typing |
75 In []: legend(['sin(x)']) |
79 In []: legend(['sin(x)']) |
76 |
80 |
77 Ok what if I want the legend to be in different location. It just requires us to define one extra parameter. |
81 We can modify previous command to specify the location of the legend, by passing an additional argument to the function. |
78 |
|
79 We have just typed legend command can we reuse it . Yes |
|
80 |
82 |
81 To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us back. |
83 To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us back. |
82 |
84 |
83 We can modify previous command to specify the location of the legend, by passing an additional argument to the function. |
|
84 #Ask madhu how to describe the feature here. |
|
85 Once you start editing a previous command and then you try to use 'Up arrow key ' you can get commands that are only similar to the command you are editing. But if you move your cursor to the beginning of the line you can get all the previous commands using up and down arrow keys. |
85 Once you start editing a previous command and then you try to use 'Up arrow key ' you can get commands that are only similar to the command you are editing. But if you move your cursor to the beginning of the line you can get all the previous commands using up and down arrow keys. |
86 In []: legend(['sin(x)'], loc = 'center') |
86 In []: legend(['sin(x)'], loc = 'center') |
87 |
87 |
88 Note that once |
88 Note that once |
89 other positions which can be tried are |
89 other positions which can be tried are |
90 'best' |
90 'best' |
91 'right' |
91 'right' |
92 |
92 |
93 Very often in mathematical plots we have to define certain points and their meaning also called annotating . We next look at how to annotate |
93 Very often in mathematical plots we have to define certain points and their meaning also called annotating . We next look at how to annotate |
94 In this case, let's add a comment at the point of origin. |
94 In this case, let's add a comment at the point of origin. |
95 In []: annotate('origin', xy=(0, 0)) |
95 In []: annotate('local max', xy=(1.5, 1)) |
96 |
96 |
97 The first argument is the comment string and second one is the position for it. |
97 The first argument is the comment string and second one is the position for it. |
98 |
98 |
99 As you can see, the boundary along the x-axis extends after the graph and there is an ugly blank space left on the right. Also along the y-axis, the sine plot in fact is cut by the boundary. We want to make the graph fit better. For this we shall use xlim() and ylim() to set the boundaries on the figure. |
99 As you can see, the boundary along the x-axis extends after the graph and there is an ugly blank space left on the right. Also along the y-axis, the sine plot in fact is cut by the boundary. We want to make the graph fit better. For this we shall use xlim() and ylim() to set the boundaries on the figure. |
100 |
100 |
107 Ok, what do I do with all this effort . I obviously have to save it . |
107 Ok, what do I do with all this effort . I obviously have to save it . |
108 |
108 |
109 We save the plot by the function savefig |
109 We save the plot by the function savefig |
110 In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. |
110 In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. |
111 |
111 |
112 #other supported formats are: eps, ps, pdf etc. |
112 other supported formats are: eps, ps, pdf etc. |
113 When we use plot again |
113 |
|
114 Let's see what happens when we use plot again |
114 In []: plot(x, cos(x)) by default plots get overlaid. |
115 In []: plot(x, cos(x)) by default plots get overlaid. |
115 |
116 |
116 we update Y axis label |
117 we update Y axis label |
117 In []: ylabel('f(x)') |
118 In []: ylabel('f(x)') |
118 |
119 |
119 Now in these situations with overlaid graphs, legend becomes absolutely essential. To add multiple legends, we pass the strings within quotes separated by commas and enclosed within square brackets as shown. |
120 Now in these situations with overlaid graphs, legend becomes absolutely essential. To add multiple legends, we pass the strings within quotes separated by commas and enclosed within square brackets as shown. |
120 |
121 |
121 In []: legend( [ 'sin(x)' , 'cos(x)'] ) |
122 In []: legend( [ 'sin(x)' , 'cos(x)'] ) |
122 |
123 |
|
124 Please note that the previous legend is overwritten. |
|
125 |
123 In []: clf() |
126 In []: clf() |
124 clears the plot area and starts afresh. |
127 clears the plot area and starts afresh. |
|
128 |
|
129 # Close the figure manually. |
125 |
130 |
126 *In case we want to create multiple plots rather than overlaid plots, we use 'figure' function. |
131 *In case we want to create multiple plots rather than overlaid plots, we use 'figure' function. |
127 The figure command is used to open a plain figure window without any plot. |
132 The figure command is used to open a plain figure window without any plot. |
128 In []: figure(1) |
133 In []: figure(1) |
129 |
134 |
153 In []: close() |
158 In []: close() |
154 |
159 |
155 close() now closes the figure(2). |
160 close() now closes the figure(2). |
156 In []: close() |
161 In []: close() |
157 |
162 |
158 The plot command can take the additional parameters such as 'g' which generates the plot in green color. |
163 Now, what if we want to plot in a different color? The plot command can take the additional parameters such as 'g' which generates the plot in green color. |
159 |
164 |
160 Passing the linewidth=2 option to plot, generates the plot with linewidth of two units. |
165 What if we want a thicker line? Passing the linewidth=2 option to plot, generates the plot with linewidth of two units. |
161 Use Up arrow to get to the previous commands |
166 |
162 In []: plot(x, sin(x), 'g', linewidth=2) and add arguments |
167 In []: plot(x, sin(x), 'g', linewidth=2) and add arguments |
163 |
168 |
164 In []: clf() |
169 In []: clf() |
165 |
170 |
166 In order to plot points you may pass '.' as a parameter to plot |
171 If we want to plot points we may pass '.' as a parameter to plot |
167 In []: plot(x, sin(x), '.') |
172 In []: plot(x, sin(x), '.') |
168 |
173 |
169 In []: clf() |
174 In []: clf() |
170 |
175 |
171 You may look at more options related to colors and type of lines using plot?(question mark) |
176 You may look at more options related to colors and type of lines using plot?(question mark) |
172 |
177 There are numerous options and various combinations available. |
173 quit the documentation using 'q' |
178 quit the documentation using 'q' |
174 |
179 |
175 In []: clf() |
180 In []: clf() |
176 |
181 |
177 and finally to close the plot |
182 and finally to close the plot |