basic-plot.txt
author Shantanu <shantanu@fossee.in>
Tue, 30 Mar 2010 17:15:22 +0530
changeset 4 4dee50d4804b
parent 0 67604aed10e0
child 5 76fe6a48386f
permissions -rw-r--r--
Added changes to array.org file and basic plotting.
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
4
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     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. 
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     4
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     5
I am assuming that you have them installed on your system.
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
     6
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
     7
Lets start ipython. Open up your terminal and type the following. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
     8
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
$ ipython -pylab
4
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    10
press RETURN
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
This will give us a prompt where we can get started. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    13
4
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    14
First, we create a sequence of equally spaced points from 0 to 2*pi
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
In []: x = linspace(0, 2*pi, 100)
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
We have passed three arguments to linspace function - the first point, the last point and the total number of points. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    18
lets see what is x
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    19
In []: x
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
 x is a sequence of 100 points starting from 0 to 2*pi. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    21
In []: len(x)
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
 Shows the length of x to be 100 points. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    23
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    24
To obtain the plot we say, 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
In []: plot(x, sin(x))
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    26
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
It gives a plot of x vs y, where y is sin values of x, with the default color and line properties. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
Both 'pi' and 'sin' come from 'pylab'. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
Now that we have a basic plot, we can label and title the plot. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
In []: xlabel('x') adds a label to the x-axis. Note that 'x' is enclosed in quotes. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
In []: ylabel('sin(x)') adds a label to the y-axis. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
In []: title('Sinusoid') adds a title to the plot. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
Now we add a legend to the plot. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
In []: legend(['sin(x)'])
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    39
We can specify the location of the legend, by passing an additional argument to the function. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    40
In []: legend(['sin(2y)'], loc = 'center')
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    41
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
other positions which can be tried are 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    43
'best' 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
'right'
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    45
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    46
We now annotate, i.e add a comment at, the point with maximum sin value. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
In []: annotate('local max', xy=(1.5, 1))
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    49
The first argument is the comment and second one is the position for it. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    50
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
Now, we save the plot 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    52
In []: savefig('sin.png') saves the figure as sin.png in current directory. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
?#other supported formats are: eps, emf, ps, pdf, ps, raw, rgba, svg
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    55
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    56
and finally to close the plot
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
In []: close()
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    58
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    59
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    60
****************