1 * Script |
1 * Script |
|
2 ********** |
|
3 Some greeting - Hi or Hello or Welcome - would be polite to start with |
|
4 ********** |
2 |
5 |
3 Hello, in this tutorial, we will cover the basics of 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. |
4 |
7 |
5 I am assuming that you have them installed on your system. |
8 I am assuming that you have them installed on your system. |
6 |
9 |
7 Lets start ipython. Open up your terminal and type the following. |
10 Lets start IPython. Click Applications - Accessories - Terminal. The terminal window will open. Type the following command. |
8 |
11 |
9 $ ipython -pylab |
12 $ ipython -pylab |
10 press RETURN |
13 press RETURN |
11 |
14 |
12 This will give us a prompt where we can get started. |
15 This will give us a prompt where we can get started. |
13 |
16 |
14 First, we create a sequence of equally spaced points from 0 to 2*pi |
17 First, we create a sequence of numbers which are equally spaced starting from 0 till/to(?) 2*pi |
|
18 |
|
19 In []: x = lins<Tab> will auto complete the function. This is one of the feature of IPython. |
|
20 |
15 In []: x = linspace(0, 2*pi, 100) |
21 In []: x = linspace(0, 2*pi, 100) |
16 |
22 |
17 We have passed three arguments to linspace function - the first point, the last point and the total number of points. |
23 To check or read documentation on 'linspace' function type |
18 lets see what is x |
24 |
|
25 In []: lins<Tab>pace? |
|
26 |
|
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'. |
|
28 |
|
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. |
|
30 Check value of x by |
19 In []: x |
31 In []: x |
20 x is a sequence of 100 points starting from 0 to 2*pi. |
32 x is a sequence of 100 points starting from 0 to 2*pi. Length of x can be seen via function |
21 In []: len(x) |
33 In []: len(x) |
22 Shows the length of x to be 100 points. |
34 which shows the length of x to be 100 points. |
23 |
35 |
24 To obtain the plot we say, |
36 To obtain the plot we say, |
25 In []: plot(x, sin(x)) |
37 In []: plot(x, sin(x)) |
26 |
38 *** |
27 It gives a plot of x vs y, where y is sin values of x, with the default color and line properties. |
39 As you can see a plot has come on the screen. |
|
40 *** |
|
41 A plot of x vs sin(x) appears on screen, with the default color and line properties. |
28 |
42 |
29 Both 'pi' and 'sin' come from 'pylab'. |
43 Both 'pi' and 'sin' come from 'pylab'. |
30 |
44 |
31 Now that we have a basic plot, we can label and title the plot. |
45 Now that we have a basic plot, we can label and title the plot. |
32 In []: xlabel('x') adds a label to the x-axis. Note that 'x' is enclosed in quotes. |
46 In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. |
33 In []: ylabel('sin(x)') adds a label to the y-axis. |
47 Similarly |
34 In []: title('Sinusoid') adds a title to the plot. |
48 In []: ylabel('sin(x)') adds a label to the y-axis. |
|
49 To add a title to plot we simply use |
|
50 In []: tit<TAB>le('Sinusoid'). |
35 |
51 |
36 Now we add a legend to the plot. |
52 Now we will add a legend to the plot. |
37 In []: legend(['sin(x)']) |
53 In []: legend(['sin(x)']) |
38 |
54 |
39 We can specify the location of the legend, by passing an additional argument to the function. |
55 To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back. |
|
56 We can modify previous command to specify the location of the legend, by passing an additional argument to the function. |
40 In []: legend(['sin(2y)'], loc = 'center') |
57 In []: legend(['sin(2y)'], loc = 'center') |
41 |
58 |
42 other positions which can be tried are |
59 other positions which can be tried are |
43 'best' |
60 'best' |
44 'right' |
61 'right' |
45 |
62 |
46 We now annotate, i.e add a comment at, the point with maximum sin value. |
63 We now annotate, i.e add a comment, at the point with maximum sin value. |
47 In []: annotate('local max', xy=(1.5, 1)) |
64 In []: annotate('local max', xy=(1.5, 1)) |
48 |
65 |
49 The first argument is the comment and second one is the position for it. |
66 The first argument is the comment and second one is the position for it. |
50 |
67 |
51 Now, we save the plot |
68 Now, we save the plot as follows |
52 In []: savefig('sin.png') saves the figure as sin.png in current directory. |
69 In []: savefig('sin.png') saves the figure as sin.png in the current directory. |
53 |
70 |
54 ?#other supported formats are: eps, emf, ps, pdf, ps, raw, rgba, svg |
71 ?#other supported formats are: eps, ps, pdf etc. |
|
72 |
|
73 When we use plot again by default plots get overlaid. |
|
74 In []: plot(x, cos(x)) |
|
75 |
|
76 we update Y axis label |
|
77 In []: ylabel('f(x)') |
|
78 |
|
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. |
|
80 |
|
81 In []: legend( [ 'sin(y)' , 'cos(y)'] ) |
|
82 |
|
83 In []: clf() |
|
84 clears the plot area and start afresh. |
|
85 |
|
86 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. |
|
88 In []: figure(1) |
|
89 |
|
90 plot() plot command plots a sin plot on figure(1) |
|
91 In []: plot(y, sin(y)) |
|
92 |
|
93 to creates a new plain figure window without any plot. |
|
94 In []: figure(2) |
|
95 figure() also shifts the focus between multiple windows. |
|
96 |
|
97 Any command issued henceforth applies to this window only. |
|
98 In []: plot(x, cos(x)) |
|
99 The previous plot window remains unchanged to these commands. |
|
100 |
|
101 In []: savefig('cosine.png') |
|
102 |
|
103 figure(1) shifts the focus back to figure(1). |
|
104 In []: figure(1) |
|
105 |
|
106 title() sets the title of figure(1) |
|
107 In []: title('sin(y)') |
|
108 |
|
109 Here we save the plot of figure(1). |
|
110 In []: savefig('sine.png') |
|
111 |
|
112 close() closes figure(1). Now there is just one figure that is open and hence |
|
113 the focus is automatically shifted to figure(2). |
|
114 In []: close() |
|
115 |
|
116 close() now closes the figure(2). |
|
117 In []: close() |
|
118 |
|
119 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 |
|
121 In []: plot(x, cos(x), 'r') and add argument. |
|
122 |
|
123 # For other color options you may check out 'plot?' |
|
124 |
|
125 In []: clf() |
|
126 |
|
127 Passing the linewidth=2 option to plot, generates the plot with linewidth of two units. |
|
128 In []: plot(x, sin(x), 'g', linewidth=2) |
|
129 |
|
130 In []: clf() |
|
131 |
|
132 In order to plot points in black color you can pass 'k.' parameter to plot |
|
133 In []: plot(x, , 'k.') |
|
134 |
|
135 In []: clf() |
|
136 |
|
137 A plot using dashed lines can be generated by passing the '--' parameter |
|
138 In []: plot(x, y, '--') |
|
139 |
|
140 You may look at more options related to colors and type of lines using plot? |
|
141 |
|
142 In []: clf() |
55 |
143 |
56 and finally to close the plot |
144 and finally to close the plot |
57 In []: close() |
145 In []: close() |
58 |
146 |
59 |
|
60 **************** |
147 **************** |
|
148 This brings us to the end of this tutorial. This tutorial is first in the series of Python for Scientific Computing Tutorials. |
|
149 **************** |