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, in this tutorial, we will cover the basics of the Plotting features available in Python. We shall use Ipython and pylab. Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion, help etc. pylab is python library which provides plotting functionality. |
6 Hello, in this tutorial, we will cover the basics of the Plotting features available in Python. We shall use Ipython and pylab. Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion, help etc. pylab is python library which provides plotting functionality. |
7 |
7 |
8 I am assuming that you have them installed on your system. |
8 I am assuming that you have both of them installed on your system. |
9 |
|
10 Lets start IPython. Click Applications - Accessories - Terminal. The terminal window will open. Type the following command. |
|
11 |
9 |
12 $ ipython -pylab |
10 $ ipython -pylab |
13 press RETURN |
11 press RETURN |
14 |
12 |
15 This will give us a prompt where we can get started. |
13 This will give us a prompt where we can get started. |
16 |
14 |
17 First, we create a sequence of numbers which are equally spaced starting from 0 till/to(?) 2*pi |
15 First, we create a sequence of equally spaced points starting from 0 to 2*pi |
18 |
16 |
19 In []: x = lins<Tab> will auto complete the function. This is one of the feature of IPython. |
17 In []: x = lins<Tab> will automatically complete the function. This is one of the many useful features of IPython. |
20 |
18 |
21 In []: x = linspace(0, 2*pi, 100) |
19 In []: x = linspace(0, 2*pi, 100) |
22 |
20 |
23 To check or read documentation on 'linspace' function type |
21 To know more about the 'linspace' function type |
24 |
22 |
25 In []: lins<Tab>pace? |
23 In []: lins<Tab>pace? |
26 |
24 |
27 It shows documentation related to linspace function. 'help' talks in detail about arguments to be passed, return values, some examples on usage. (To scroll down the page use 'SPACE' key and to scroll up use 'b')To navigate through content use arrow(/Page Up and Page Down) keys. ':See Also' section hints about other related or similar functions which might be useful. To exit help (mode) press 'q'. |
25 It shows documentation related to linspace function. 'help' talks in detail about arguments to be passed, return values, some examples on usage. (To scroll down the page use 'SPACE' key and to scroll up use 'b')To navigate through content use arrow(/Page Up and Page Down) keys. ':See Also' section hints about other related or similar functions which might be useful. To exit help (mode) press 'q'. |
28 |
26 |
29 In our case, we have passed three arguments to the linspace function - the starting point, the last point and the total number of points. |
27 In this case, we have passed three arguments to the linspace function - the starting point, the last point and the total number of points. |
30 Check value of x by |
28 Check value of x by |
31 In []: x |
29 In []: x |
32 x is a sequence of 100 points starting from 0 to 2*pi. Length of x can be seen via function |
30 x is a sequence of 100 points starting from 0 to 2*pi. Length of x can be seen via function |
33 In []: len(x) |
31 In []: len(x) |
34 which shows the length of x to be 100 points. |
32 which shows it to be 100 points. |
35 |
33 |
36 To obtain the plot we say, |
34 To obtain the plot we say, |
37 In []: plot(x, sin(x)) |
35 In []: plot(x, sin(x)) |
38 *** |
36 *** |
39 As you can see a plot has come on the screen. |
37 As you can see a plot has appeared on the screen. |
40 *** |
38 *** |
41 A plot of x vs sin(x) appears on screen, with the default color and line properties. |
39 The plot has the default color and line properties. |
42 |
40 |
43 Both 'pi' and 'sin' come from 'pylab'. |
41 Both 'pi' and 'sin' come from 'pylab'. |
44 |
42 |
45 Now that we have a basic plot, we can label and title the plot. |
43 Now that we have a basic plot, we can label and title the plot. |
46 In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. |
44 In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. |
58 |
56 |
59 other positions which can be tried are |
57 other positions which can be tried are |
60 'best' |
58 'best' |
61 'right' |
59 'right' |
62 |
60 |
63 We now annotate, i.e add a comment, at the point with maximum sin value. |
61 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. |
64 In []: annotate('local max', xy=(1.5, 1)) |
62 In []: annotate('local max', xy=(1.5, 1)) |
65 |
63 |
66 The first argument is the comment and second one is the position for it. |
64 The first argument is the comment string and second one is the position for it. |
67 |
65 |
68 Now, we save the plot as follows |
66 Now, we save the plot as follows |
69 In []: savefig('sin.png') saves the figure as sin.png in the current directory. |
67 In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. |
70 |
68 |
71 ?#other supported formats are: eps, ps, pdf etc. |
69 #other supported formats are: eps, ps, pdf etc. |
72 |
70 |
73 When we use plot again by default plots get overlaid. |
71 When we use plot again by default plots get overlaid. |
74 In []: plot(x, cos(x)) |
72 In []: plot(x, cos(x)) |
75 |
73 |
76 we update Y axis label |
74 we update Y axis label |
77 In []: ylabel('f(x)') |
75 In []: ylabel('f(x)') |
78 |
76 |
79 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. |
77 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. |
80 |
78 |
81 In []: legend( [ 'sin(y)' , 'cos(y)'] ) |
79 In []: legend( [ 'sin(x)' , 'cos(x)'] ) |
82 |
80 |
83 In []: clf() |
81 In []: clf() |
84 clears the plot area and start afresh. |
82 clears the plot area and starts afresh. |
85 |
83 |
86 In case we want to create multiple plots rather than overlaid plots, we use 'figure' function. |
84 In case we want to create multiple plots rather than overlaid plots, we use 'figure' function. |
87 The figure command is used to open a plain figure window without any plot. |
85 The figure command is used to open a plain figure window without any plot. |
88 In []: figure(1) |
86 In []: figure(1) |
89 |
87 |
90 plot() plot command plots a sin plot on figure(1) |
88 plot() plot command plots a sin plot on figure(1) |
91 In []: plot(y, sin(y)) |
89 In []: plot(x, sin(x)) |
92 |
90 |
93 to creates a new plain figure window without any plot. |
91 to creates a new plain figure window without any plot. |
94 In []: figure(2) |
92 In []: figure(2) |
95 figure() also shifts the focus between multiple windows. |
93 figure() is also used to shift the focus between multiple windows. |
96 |
94 |
97 Any command issued henceforth applies to this window only. |
95 Any command issued henceforth applies to this window only. |
98 In []: plot(x, cos(x)) |
96 In []: plot(x, cos(x)) |
99 The previous plot window remains unchanged to these commands. |
97 The previous plot window remains unchanged to these commands. |
100 |
98 |
118 |
116 |
119 The plot command takes the following optional parameters such as 'r' which generates the plot in red color. |
117 The plot command takes the following optional parameters such as 'r' which generates the plot in red color. |
120 Use up arrow key to get till this command |
118 Use up arrow key to get till this command |
121 In []: plot(x, cos(x), 'r') and add argument. |
119 In []: plot(x, cos(x), 'r') and add argument. |
122 |
120 |
123 # For other color options you may check out 'plot?' |
|
124 |
|
125 In []: clf() |
121 In []: clf() |
126 |
122 |
127 Passing the linewidth=2 option to plot, generates the plot with linewidth of two units. |
123 Passing the linewidth=2 option to plot, generates the plot with linewidth of two units. |
128 In []: plot(x, sin(x), 'g', linewidth=2) |
124 In []: plot(x, sin(x), 'g', linewidth=2) |
129 |
125 |
130 In []: clf() |
126 In []: clf() |
131 |
127 |
132 In order to plot points in black color you can pass 'k.' parameter to plot |
128 In order to plot points you may pass '.' as a parameter to plot |
133 In []: plot(x, , 'k.') |
129 In []: plot(x, sin(x), '.') |
|
130 |
|
131 In []: clf() |
|
132 |
|
133 In order to plot filled circles in black color you can pass 'ko' parameter to plot |
|
134 In []: plot(x, cos(x), 'ko') |
134 |
135 |
135 In []: clf() |
136 In []: clf() |
136 |
137 |
137 A plot using dashed lines can be generated by passing the '--' parameter |
138 A plot using dashed lines can be generated by passing the '--' parameter |
138 In []: plot(x, y, '--') |
139 In []: plot(x, y, '--') |