1 * Script |
1 * Script |
2 ********** |
|
3 Some greeting-- Hi or Hello or Welcome - would be polite to start with |
|
4 ********** |
|
5 |
2 |
6 *Hello and welcome to the tutorial on Basic Plotting using Python. This is the first tutorial in a series of tutorials on Python for Scientific Computing. This tutorial is created by the FOSSEE team, IIT Bombay . |
3 *Hello and welcome to the tutorial on Basic Plotting using Python. This is the first tutorial in a series of tutorials on Python for Scientific Computing. This tutorial is created by the FOSSEE team, IIT Bombay . |
7 |
4 |
8 *The intended audience for this tutorial are Engineering, mathematics and science teachers and students |
5 *The intended audience for this tutorial are Engineering, mathematics and science teachers and students |
9 |
6 |
10 *The goals are to |
7 *The goals are to |
11 help one use Python as a basic plotting tool. |
8 help one use Python as a basic plotting tool. |
12 and understand python as a scripting language. |
|
13 |
|
14 |
9 |
15 *In this tutorial, we will cover the basics of the Plotting features available in Python. |
10 *In this tutorial, we will cover the basics of the Plotting features available in Python. |
16 For this we shall use Ipython and pylab. |
11 For this we shall use Ipython and pylab. |
17 Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion,easier access to help , and many other useful features which are not present in the vanilla Python interpreter. |
12 Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion,easier access to help , and many other useful features which are not present in the vanilla Python interpreter. |
18 Pylab is python library which provides plotting functionality. |
13 Pylab is python library which provides plotting functionality. |
40 First, we will create a sequence of equally spaced points starting from 0 to 2*pi , we will use function linspace for that |
35 First, we will create a sequence of equally spaced points starting from 0 to 2*pi , we will use function linspace for that |
41 |
36 |
42 Type: |
37 Type: |
43 In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word |
38 In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word |
44 |
39 |
45 In [] x=linspace( |
40 In [] x=linspace( RETURN |
46 oops I made a mistake . As you can see I made the mistake of not writing command correctly |
41 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 |
42 and Ipython changed the prompt . To get the old prompt back type crtl-c |
48 |
43 |
49 In []: x = linspace(0, 2*pi, 50) |
44 In []: x = linspace(0, 2*pi, 50) |
50 |
45 |
51 |
46 |
52 To obtain the plot we say, |
47 To obtain the plot we use, |
53 In []: plot(x, sin(x)) |
48 In []: plot(x, sin(x)) |
54 *** |
49 *** |
55 As you can see a plot has appeared on the screen. |
50 As you can see a plot has appeared on the screen. |
56 *** |
51 *** |
57 |
52 |
59 |
54 |
60 *To know more about any function, example for the 'linspace' function you can type ? after it . |
55 *To know more about any function, example for the 'linspace' function you can type ? after it . |
61 |
56 |
62 In []: linspace? |
57 In []: linspace? |
63 |
58 |
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 arrows , pageup and pagedown keys . |
59 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. |
65 At any time you want to come out of the help use q key . |
60 At any time you want to come out of the help use 'q' key. |
66 See how easy it is to get help in python . |
61 See how easy it is to get help in IPython. |
67 |
62 |
68 *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 |
63 *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 |
69 |
64 |
70 |
65 |
71 In this case we have used two commands |
66 In this case we have used two commands |
72 'pi' and 'sin' these come from 'pylab'library called using -pylab. |
67 'pi' and 'sin' these come from 'pylab' library called using -pylab. |
73 |
68 |
74 *Now that we have a basic plot, we can label and title the plot. |
69 *Now that we have a basic plot, we can label and title the plot. |
75 In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. |
70 In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. |
76 Similarly |
71 Similarly |
77 In []: ylabel('sin(x)') adds a label to the y-axis. |
72 In []: ylabel('sin(x)') adds a label to the y-axis. |
78 To add a title to plot we simply use |
73 To add a title to plot we simply use |
79 In []: tit<TAB>le('Sinusoid'). |
74 In []: tit<TAB>le('Sinusoid'). |
80 |
75 |
81 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. |
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. |
82 |
77 |
83 Add a legend to the plot by typing |
78 Add a legend to the plot by typing |
84 In []: legend(['sin(x)']) |
79 In []: legend(['sin(x)']) |
85 |
80 |
86 Ok what if I want the legend to be in the centre . It just requires us to define one extra parameter. |
81 Ok what if I want the legend to be in different location. It just requires us to define one extra parameter. |
87 |
82 |
88 We have just typed legend command can we reuse it . Yes |
83 We have just typed legend command can we reuse it . Yes |
89 |
84 |
90 To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back. |
85 To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back. |
91 |
86 |
92 We can modify previous command to specify the location of the legend, by passing an additional argument to the function. |
87 We can modify previous command to specify the location of the legend, by passing an additional argument to the function. |
93 #Ask madhu how to describe the feature here. |
88 #Ask madhu how to describe the feature here. |
94 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 . |
89 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. |
95 In []: legend(['sin(x)'], loc = 'center') |
90 In []: legend(['sin(x)'], loc = 'center') |
96 |
91 |
97 Note that once |
92 Note that once |
98 other positions which can be tried are |
93 other positions which can be tried are |
99 'best' |
94 'best' |
100 'right' |
95 'right' |
101 |
96 |
102 |
97 Very often in mathematical plots we have to define certain points and their meaning also called annotating . We next look at how to annotate |
103 Very often in mathematical plots we have define certain points abd there meaning also called annotating . We next look at how to annotate |
|
104 In this case, let's add a comment at the point of origin. |
98 In this case, let's add a comment at the point of origin. |
105 In []: annotate('origin', xy=(0, 0)) |
99 In []: annotate('origin', xy=(0, 0)) |
106 |
100 |
107 The first argument is the comment string and second one is the position for it. |
101 The first argument is the comment string and second one is the position for it. |
108 |
102 |
109 Ok, what do I do with all this effort . I obviously have to save it . |
103 Ok, what do I do with all this effort . I obviously have to save it . |
110 |
104 |
111 We save the plot by the function savefig |
105 We save the plot by the function savefig |
112 In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. |
106 In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. |
113 |
107 |
114 |
|
115 |
|
116 |
|
117 #other supported formats are: eps, ps, pdf etc. |
108 #other supported formats are: eps, ps, pdf etc. |
118 |
|
119 When we use plot again by default plots get overlaid. |
109 When we use plot again by default plots get overlaid. |
120 In []: plot(x, cos(x)) |
110 In []: plot(x, cos(x)) |
121 |
111 |
122 we update Y axis label |
112 we update Y axis label |
123 In []: ylabel('f(x)') |
113 In []: ylabel('f(x)') |