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