1 * Script |
1 * Script |
2 ********** |
2 ********** |
3 Some greeting-- Hi or Hello or Welcome - would be polite to start with |
3 Some greeting-- Hi or Hello or Welcome - would be polite to start with |
4 ********** |
4 ********** |
5 |
5 |
6 Hello and welcome to the first tutorial in a series of tutorials on Python for Scientific Computing . |
6 *Hello and welcome to this tutorial on Basic Plotting using Python in a series of tutorials on Python for Scientific Computing .This tutorial is created by the FOSSEE team , IIT Bombay . |
7 In this tutorial, we will cover the basics of the Plotting features available in Python. |
7 |
|
8 *The intended audience for this tutorial are Engineering , mathematics and science teachers and students |
|
9 |
|
10 *The goals are to |
|
11 help one use Python as a basic plotting tool. |
|
12 and understand python as a scripting language. |
|
13 |
|
14 |
|
15 *In this tutorial, we will cover the basics of the Plotting features available in Python. |
8 For this we shall use Ipython and pylab. |
16 For this we shall use Ipython and pylab. |
9 Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion,easier access to help , and many other useful features. |
17 Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion,easier access to help , and many other useful features. |
10 Pylab is python library which provides plotting functionality. |
18 Pylab is python library which provides plotting functionality. |
11 |
19 |
12 I am assuming that you have both Ipython and Pylab installed on your system . |
20 I am assuming that you have both Ipython and Pylab installed on your system . |
13 |
21 |
14 On your terminal type in the command Ipython -pylab |
22 *On your terminal type in the command Ipython -pylab |
15 $ ipython -pylab |
23 $ ipython -pylab |
16 press RETURN |
24 press RETURN |
17 |
25 |
18 We will first start with the absolute basic i.e how to print hello world |
26 We will first start with the absolute basic i.e how to print hello world |
19 |
27 |
20 In []: print 'hello world' |
28 In []: print 'hello world' |
21 |
29 |
22 Now to exit ipython type Ctrl-D . It will ask if you wish to exit type y to exit |
30 Voila we have got hello world output |
23 |
31 |
24 Now we will get back to plotting . |
32 To exit ipython type Ctrl-D . It will ask if you wish to exit ipython . |
25 |
33 |
26 type : |
34 *Now we will get back to plotting . |
|
35 |
|
36 type again : |
27 $ ipython -pylab |
37 $ ipython -pylab |
28 press RETURN |
38 press RETURN |
29 |
39 |
30 First, we will create a sequence of equally spaced points starting from 0 to 2*pi |
40 First, we will create a sequence of equally spaced points starting from 0 to 2*pi , we will use function linspace for that |
31 |
41 |
32 For this type: |
42 Type: |
|
43 In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word |
|
44 |
|
45 In [] x=linspace( |
|
46 oops I made a mistake . As you can see I made the mistake of not writing command correctly |
|
47 and Ipython changed the prompt . To get the old prompt back type crtl-c |
33 |
48 |
34 In []: x = linspace(0, 2*pi, 50) |
49 In []: x = linspace(0, 2*pi, 50) |
35 |
50 |
36 You can also do : |
|
37 In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 To know more about the 'linspace' function type |
|
43 |
|
44 In []: lins<Tab>pace linspace? |
|
45 |
|
46 It shows documentation related to linspace function. 'help' talks in detail about arguments to be passed, return values, some examples on usage. |
|
47 |
|
48 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 |
|
49 |
|
50 |
|
51 |
|
52 You can Check value of x by |
|
53 In []: x |
|
54 x is a sequence of 50 points starting from 0 to 2*pi. Length of x can be seen via function |
|
55 In []: len(x) |
|
56 which shows it to be 50 points. |
|
57 |
51 |
58 To obtain the plot we say, |
52 To obtain the plot we say, |
59 In []: plot(x, sin(x)) |
53 In []: plot(x, sin(x)) |
60 *** |
54 *** |
61 As you can see a plot has appeared on the screen. |
55 As you can see a plot has appeared on the screen. |
62 *** |
56 *** |
|
57 |
63 The plot has the default color and line properties. |
58 The plot has the default color and line properties. |
64 |
59 |
65 Both 'pi' and 'sin' come from 'pylab'. |
60 *To know more about any function, example for the 'linspace' function you can type ? after it . |
66 |
61 |
67 Now that we have a basic plot, we can label and title the plot. |
62 In []: linspace? |
|
63 |
|
64 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 , down and pageup and pagedown arrows and q for quitting . See how easy to get help in python . |
|
65 |
|
66 *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 |
|
67 |
|
68 |
|
69 In this case we have used two commands |
|
70 'pi' and 'sin' these come from 'pylab'library called using -pylab. |
|
71 |
|
72 *Now that we have a basic plot, we can label and title the plot. |
68 In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. |
73 In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. |
69 Similarly |
74 Similarly |
70 In []: ylabel('sin(x)') adds a label to the y-axis. |
75 In []: ylabel('sin(x)') adds a label to the y-axis. |
71 To add a title to plot we simply use |
76 To add a title to plot we simply use |
72 In []: tit<TAB>le('Sinusoid'). |
77 In []: tit<TAB>le('Sinusoid'). |
73 |
78 |
74 Now we will add a legend to the plot. |
79 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 seeing this unsatisfied , it lacks a legend. |
|
80 |
|
81 Add a legend to the plot by typing |
75 In []: legend(['sin(x)']) |
82 In []: legend(['sin(x)']) |
76 |
83 |
|
84 Ok what if I want the legend to be in the centre . It just requires us to define one extra parameter. |
|
85 |
|
86 We have just typed legend command can we reuse it . Yes |
|
87 |
77 To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back. |
88 To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back. |
|
89 |
78 We can modify previous command to specify the location of the legend, by passing an additional argument to the function. |
90 We can modify previous command to specify the location of the legend, by passing an additional argument to the function. |
|
91 #Ask madhu how to describe the feature here. |
79 In []: legend(['sin(x)'], loc = 'center') |
92 In []: legend(['sin(x)'], loc = 'center') |
80 |
93 |
|
94 Note that once |
81 other positions which can be tried are |
95 other positions which can be tried are |
82 'best' |
96 'best' |
83 'right' |
97 'right' |
84 |
98 |
85 We now annotate, i.e add a comment, at a specific position in the graph. In this case, let's add a comment at the point with maximum sin value. |
99 |
|
100 Very often in mathematical plots we have define certain points abd there meaning also called annotating . We next look at how to annotate |
|
101 In this case, let's add a comment at the point of origin. |
86 In []: annotate('origin', xy=(0, 0)) |
102 In []: annotate('origin', xy=(0, 0)) |
87 |
103 |
88 The first argument is the comment string and second one is the position for it. |
104 The first argument is the comment string and second one is the position for it. |
89 |
105 |
90 Now, we save the plot as follows |
106 Ok, what do I do with all this effort . I obviously have to save it . |
|
107 |
|
108 We save the plot by the function savefig |
91 In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. |
109 In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. |
|
110 |
|
111 |
|
112 |
92 |
113 |
93 #other supported formats are: eps, ps, pdf etc. |
114 #other supported formats are: eps, ps, pdf etc. |
94 |
115 |
95 When we use plot again by default plots get overlaid. |
116 When we use plot again by default plots get overlaid. |
96 In []: plot(x, cos(x)) |
117 In []: plot(x, cos(x)) |
103 In []: legend( [ 'sin(x)' , 'cos(x)'] ) |
124 In []: legend( [ 'sin(x)' , 'cos(x)'] ) |
104 |
125 |
105 In []: clf() |
126 In []: clf() |
106 clears the plot area and starts afresh. |
127 clears the plot area and starts afresh. |
107 |
128 |
108 In case we want to create multiple plots rather than overlaid plots, we use 'figure' function. |
129 |
|
130 |
|
131 *In case we want to create multiple plots rather than overlaid plots, we use 'figure' function. |
109 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. |
110 In []: figure(1) |
133 In []: figure(1) |
111 |
134 |
112 plot() plot command plots a sin plot on figure(1) |
135 We will use plot() plot again to plot a sin curve on figure(1) |
113 In []: plot(x, sin(x)) |
136 In []: plot(x, sin(x)) |
114 |
137 |
115 to creates a new plain figure window without any plot. |
138 to creates a new plain figure window without any plot type: |
116 In []: figure(2) |
139 In []: figure(2) |
117 figure() is also used to shift the focus between multiple windows. |
140 figure() is also used to shift the focus between multiple windows. |
118 |
141 |
119 Any command issued henceforth applies to this window only. |
142 Any command issued henceforth applies to this window only so typing |
120 In []: plot(x, cos(x)) |
143 In []: plot(x, cos(x)) |
|
144 plots cos curve on second window now. |
121 The previous plot window remains unchanged to these commands. |
145 The previous plot window remains unchanged to these commands. |
122 |
146 |
123 In []: savefig('cosine.png') |
|
124 |
147 |
125 figure(1) shifts the focus back to figure(1). |
148 |
|
149 calling function figure using argument 1 shifts the focus back to figure(1). |
126 In []: figure(1) |
150 In []: figure(1) |
127 |
151 |
128 title() sets the title of figure(1) |
152 title() sets the title of figure(1) |
129 In []: title('sin(x)') |
153 In []: title('sin(x)') |
130 |
154 |
131 Here we save the plot of figure(1). |
155 Now we save the plot of figure(1) by typing |
132 In []: savefig('sine.png') |
156 In []: savefig('sine.png') |
133 |
157 |
134 close() closes figure(1). Now there is just one figure that is open and hence |
158 close() closes figure(1). Now there is just one figure that is open and hence |
135 the focus is automatically shifted to figure(2). |
159 the focus is automatically shifted to figure(2). |
136 In []: close() |
160 In []: close() |
137 |
161 |
138 close() now closes the figure(2). |
162 close() now closes the figure(2). |
139 In []: close() |
163 In []: close() |
140 |
164 |
141 The plot command takes the following optional parameters such as 'g' which generates the plot in green color. |
165 The plot command takes the parameters such as 'g' which generates the plot in green color. |
142 |
166 |
143 Passing the linewidth=2 option to plot, generates the plot with linewidth of two units. |
167 Passing the linewidth=2 option to plot, generates the plot with linewidth of two units. |
144 Use Up arrow to get to the previous commands |
168 Use Up arrow to get to the previous commands |
145 In []: plot(x, sin(x), 'g', linewidth=2) and add arguments |
169 In []: plot(x, sin(x), 'g', linewidth=2) and add arguments |
146 |
170 |