basic-plot.txt
author asokan <asokan@fossee.in>
Tue, 18 May 2010 15:40:17 +0530
changeset 126 2eac725a5766
parent 31 7b685ae49b57
permissions -rw-r--r--
changes to array.txt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
* Script
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
22
273d193d1504 basic plotting script changed.
Shantanu <shantanu@fossee.in>
parents: 19
diff changeset
     3
*Hello and welcome to the tutorial on Basic Plotting using Python. 
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
     4
18
27ba96db9d12 Madhu's suggestion added
amit@thunder
parents: 17
diff changeset
     5
*The intended audience for this tutorial are Engineering, mathematics and science teachers and students
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
     6
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
     7
*In this tutorial, we will cover the basics of the Plotting features available in Python. 
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
     8
For this we shall use  Ipython and pylab. 
18
27ba96db9d12 Madhu's suggestion added
amit@thunder
parents: 17
diff changeset
     9
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.
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    10
Pylab is python library which provides plotting functionality. 
4
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    11
30
143d020bbd52 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 29
diff changeset
    12
I am assuming that both Ipython and Pylab are installed on your system. If not please refer to our tutorial on how to install IPython and pylab. 
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    13
27
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
    14
*On your terminal type in the command
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
$ ipython -pylab
4
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    16
press RETURN
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
27
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
    18
We will first start with the customary Hello world program by writing:
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    19
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    20
In []: print 'hello world'
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    21
18
27ba96db9d12 Madhu's suggestion added
amit@thunder
parents: 17
diff changeset
    22
Voila we have got hello world as the output 
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    23
18
27ba96db9d12 Madhu's suggestion added
amit@thunder
parents: 17
diff changeset
    24
To exit ipython press Ctrl-D.
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
18
27ba96db9d12 Madhu's suggestion added
amit@thunder
parents: 17
diff changeset
    26
*Now we will get back to plotting.
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    27
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    28
type again :
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    29
$ ipython -pylab
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    30
press RETURN
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    31
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    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
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    33
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    34
Type:
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    35
In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    36
19
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
    37
In []  x=linspace( RETURN
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    38
Ouch! I made a mistake . As you can see I made the mistake of not writing command correctly
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    39
and Ipython changed the prompt . To get the old prompt back press crtl-c
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    40
27
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
    41
In []: x = linspace(0, 2*pi, 100)
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    42
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    43
*To know more about any function,  in this case say, for 'linspace' you can type ? after it .
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    44
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    45
In []: linspace?
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    46
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    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.
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    48
To exit from the help menu type 'q'.
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    49
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    50
*As you can see linspace can take three parameters the starting point, the ending point and the number of points.
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    51
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    52
Let us see what x contains now. Type 'x' and hit enter.
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    53
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    54
We see that x is a sequence of numbers from 0 to 2*pi. We can check the length of x by typing 
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    55
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    56
In []: len(x)
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
26
cfc86eea45b4 Changes to basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 25
diff changeset
    58
To obtain the plot we use,
cfc86eea45b4 Changes to basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 25
diff changeset
    59
In []: plot(x, sin(x))
cfc86eea45b4 Changes to basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 25
diff changeset
    60
***
cfc86eea45b4 Changes to basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 25
diff changeset
    61
As you can see a plot has appeared on the screen. 
cfc86eea45b4 Changes to basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 25
diff changeset
    62
***
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    63
26
cfc86eea45b4 Changes to basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 25
diff changeset
    64
The plot has the default color and line properties. 
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    65
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    66
In this case we have used two commands 
19
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
    67
'pi' and 'sin' these come from 'pylab' library called using -pylab. 
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    68
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    69
*Now that we have a basic plot, we can label and title the plot. 
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    70
In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    71
Similarly
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    72
In []: ylabel('sin(x)') adds a label to the y-axis.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    73
To add a title to plot we simply use 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    74
In []: tit<TAB>le('Sinusoid').
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
19
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
    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. 
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    77
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    78
Add a legend to the plot by typing 
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    79
In []: legend(['sin(x)'])
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    80
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    81
We can modify previous command to specify the location of the legend, by passing an additional argument to the function. 
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    82
27
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
    83
To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us back.
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    84
19
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
    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.
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
    86
In []: legend(['sin(x)'], loc = 'center')
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    87
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    88
Note that once 
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    89
other positions which can be tried are
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
'best' 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    91
'right'
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    92
19
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
    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
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
    94
In this case, let's add a comment at the point of origin. 
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
    95
In []: annotate('local max', xy=(1.5, 1))
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    96
9
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
    97
The first argument is the comment string and second one is the position for it. 
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    98
29
0278e84ec327 Added xlim and ylim.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 27
diff changeset
    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.
0278e84ec327 Added xlim and ylim.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 27
diff changeset
   100
0278e84ec327 Added xlim and ylim.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 27
diff changeset
   101
In []: xlim(0, 2*pi)
0278e84ec327 Added xlim and ylim.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 27
diff changeset
   102
0278e84ec327 Added xlim and ylim.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 27
diff changeset
   103
In []: ylim(-1.2, 1.2)
0278e84ec327 Added xlim and ylim.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 27
diff changeset
   104
0278e84ec327 Added xlim and ylim.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 27
diff changeset
   105
The first value passed is the lower limit and the second is the upper limit. Hence when we do xlim(0, 2*pi) the boundary is set from x-value 0 to x-value 2*pi. Similarly for the y-axis.
0278e84ec327 Added xlim and ylim.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 27
diff changeset
   106
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   107
Ok, what do I do with all this effort . I obviously have to save it . 
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   108
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   109
We save the plot by the function savefig
9
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   110
In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. 
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   111
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   112
other supported formats are: eps, ps, pdf etc.
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   113
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   114
Let's see what happens when we use plot again 
27
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   115
In []: plot(x, cos(x)) by default plots get overlaid.
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   116
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   117
we update Y axis label 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   118
In []: ylabel('f(x)')
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   119
9
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   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.
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   121
9
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   122
In []: legend( [ 'sin(x)' , 'cos(x)'] )
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   123
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   124
Please note that the previous legend is overwritten.
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   125
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   126
In []: clf()
9
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   127
clears the plot area and starts afresh.
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   128
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   129
# Close the figure manually.
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   130
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   131
*In case we want to create multiple plots rather than overlaid plots, we use 'figure' function.
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   132
The figure command is used to open a plain figure window without any plot.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   133
In []: figure(1)
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   134
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   135
We will use plot() plot again to plot a sin curve on figure(1)
9
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   136
In []: plot(x, sin(x))
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   137
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   138
to creates a new plain figure window without any plot type: 
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   139
In []: figure(2)
9
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   140
figure() is also used to shift the focus between multiple windows. 
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   141
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   142
Any command issued henceforth applies to this window only so typing
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   143
In []: plot(x, cos(x))
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   144
plots cos curve on second window now.
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   145
The previous plot window remains unchanged to these commands.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   146
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   147
calling function figure using argument 1 shifts the focus back to figure(1).
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   148
In []: figure(1)
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   149
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   150
title() sets the title of figure(1) 
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
   151
In []: title('sin(x)')
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   152
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   153
Now we save the plot of figure(1) by typing 
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   154
In []: savefig('sine.png')
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   155
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   156
close() closes figure(1). Now there is just one figure that is open and hence 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   157
the focus is automatically shifted to figure(2).
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   158
In []: close()
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   159
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   160
close() now closes the figure(2).
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   161
In []: close()
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   162
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   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. 
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   164
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   165
What if we want a thicker line? Passing the linewidth=2 option to plot, generates the plot with linewidth of two units.
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   166
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
   167
In []: plot(x, sin(x), 'g', linewidth=2) and add arguments
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   168
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   169
In []: clf()
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   170
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   171
If we want to  plot points we may pass '.' as a parameter to plot
9
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   172
In []: plot(x, sin(x), '.')
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   173
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   174
In []: clf()
538f59bb598c Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
   175
16
cdcad776b360 Changed according Prabhu sirs comments on the script
amit@thunder
parents: 13
diff changeset
   176
You may look at more options related to colors and type of lines using plot?(question mark)
31
7b685ae49b57 Major edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 30
diff changeset
   177
There are numerous options and various combinations available.
19
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
   178
quit the documentation using 'q'
17
4ec2ba3d96f5 Encorporated changes based on comments by reviewers
amit@thunder
parents: 16
diff changeset
   179
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   180
In []: clf()
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   181
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   182
and finally to close the plot
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   183
In []: close()
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   184
27
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   185
In this tutorial we learned 
17
4ec2ba3d96f5 Encorporated changes based on comments by reviewers
amit@thunder
parents: 16
diff changeset
   186
27
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   187
Some IPython Features
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   188
Starting and exiting.
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   189
autocompletion.
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   190
help functionality.
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
   191
27
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   192
Regarding plotting we covered:
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   193
How to create basic plots.
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   194
Adding labels, legends annotation etc.
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   195
How to change looks of the plot like colors, linewidth formats etc
11
eafc653206d8 Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents: 9
diff changeset
   196
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   197
****************
22
273d193d1504 basic plotting script changed.
Shantanu <shantanu@fossee.in>
parents: 19
diff changeset
   198
This brings us to the end of this tutorial. 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. Hope you enjoyed it and found it useful.
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   199
****************
19
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
   200
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
   201
************
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
   202
A slide of review of what has been covered and sequentially/rapidly going through them.
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
   203
************
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
   204
b77b9fce62d6 Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents: 18
diff changeset
   205
Various problems of Ipython, navigation and enter, exit.
27
5712cc7589f9 Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents: 26
diff changeset
   206
What about xlim and ylim?