basic-plot.txt
changeset 11 eafc653206d8
parent 9 538f59bb598c
child 13 847f9aefa7d4
equal deleted inserted replaced
9:538f59bb598c 11:eafc653206d8
     1 * Script
     1 * Script
     2 **********
     2 **********
     3 Some greeting - Hi or Hello or Welcome - would be polite to start with
     3 Some greeting-- Hi or Hello or Welcome - would be polite to start with
     4 **********
     4 **********
     5 
     5 
     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. 
     6 Hello and welcome to the first tutorial in a series of tutorials on Python for Scientific Computing . 
       
     7 In this tutorial, we will cover the basics of the Plotting features available in Python. 
       
     8 For this we shall use  Ipython and pylab. 
       
     9 Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion,easier access to help , and many other useful features.
       
    10 Pylab is python library which provides plotting functionality. 
     7 
    11 
     8 I am assuming that you have both of them installed on your system.
    12 I am assuming that you have both Ipython and Pylab installed on your system .
     9 
    13 
       
    14 On your terminal type in the command Ipython -pylab
    10 $ ipython -pylab
    15 $ ipython -pylab
    11 press RETURN
    16 press RETURN
    12 
    17 
    13 This will give us a prompt where we can get started. 
    18 We will first start with the absolute basic i.e how to print hello world
    14 
    19 
    15 First, we create a sequence of equally spaced points starting from 0 to 2*pi
    20 In []: print 'hello world'
    16 
    21 
    17 In []: x = lins<Tab> will automatically complete the function. This is one of the many useful features of IPython.
    22 Now to exit ipython type Ctrl-D . It will ask if you wish to exit type y to exit
    18 
    23 
    19 In []: x = linspace(0, 2*pi, 100)
    24 Now we will get back to plotting .
       
    25 
       
    26 type :
       
    27 $ ipython -pylab
       
    28 press RETURN
       
    29 
       
    30 First, we will create a sequence of equally spaced points starting from 0 to 2*pi
       
    31 
       
    32 For this type:
       
    33 
       
    34 In []: x = linspace(0, 2*pi, 50)
       
    35 
       
    36 You can also do :
       
    37 In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word
       
    38 
       
    39 
       
    40 
    20 
    41 
    21 To know more about the 'linspace' function type
    42 To know more about the 'linspace' function type
    22 
    43 
    23 In []: lins<Tab>pace?
    44 In []: lins<Tab>pace  linspace?
    24 
    45 
    25 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'.
    46 It shows documentation related to linspace function. 'help' talks in detail about arguments to be passed, return values, some examples on usage.
    26 
    47 
    27 In this case, we have passed three arguments to the linspace function - the starting point, the last point and the total number of points. 
    48 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
    28 Check value of x by
    49  
       
    50 
       
    51 
       
    52 You can Check value of x by
    29 In []: x
    53 In []: x
    30  x is a sequence of 100 points starting from 0 to 2*pi. Length of x can be seen via function
    54  x is a sequence of 50 points starting from 0 to 2*pi. Length of x can be seen via function
    31 In []: len(x)
    55 In []: len(x)
    32 which shows it to be 100 points.
    56 which shows it to be 50 points.
    33 
    57 
    34 To obtain the plot we say,
    58 To obtain the plot we say,
    35 In []: plot(x, sin(x))
    59 In []: plot(x, sin(x))
    36 ***
    60 ***
    37 As you can see a plot has appeared on the screen. 
    61 As you can see a plot has appeared on the screen. 
    50 Now we will add a legend to the plot. 
    74 Now we will add a legend to the plot. 
    51 In []: legend(['sin(x)'])
    75 In []: legend(['sin(x)'])
    52 
    76 
    53 To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back.
    77 To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back.
    54 We can modify previous command to specify the location of the legend, by passing an additional argument to the function. 
    78 We can modify previous command to specify the location of the legend, by passing an additional argument to the function. 
    55 In []: legend(['sin(2y)'], loc = 'center')
    79 In []: legend(['sin(x)'], loc = 'center')
    56 
    80 
    57 other positions which can be tried are
    81 other positions which can be tried are
    58 'best' 
    82 'best' 
    59 'right'
    83 'right'
    60 
    84 
    61 We now annotate, i.e add a comment, at a specific position in the graph. In this case, let's add a comment at the point with maximum sin value. 
    85 We now annotate, i.e add a comment, at a specific position in the graph. In this case, let's add a comment at the point with maximum sin value. 
    62 In []: annotate('local max', xy=(1.5, 1))
    86 In []: annotate('origin', xy=(0, 0))
    63 
    87 
    64 The first argument is the comment string and second one is the position for it. 
    88 The first argument is the comment string and second one is the position for it. 
    65 
    89 
    66 Now, we save the plot as follows
    90 Now, we save the plot as follows
    67 In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. 
    91 In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. 
   100 
   124 
   101 figure(1) shifts the focus back to figure(1).
   125 figure(1) shifts the focus back to figure(1).
   102 In []: figure(1)
   126 In []: figure(1)
   103 
   127 
   104 title() sets the title of figure(1) 
   128 title() sets the title of figure(1) 
   105 In []: title('sin(y)')
   129 In []: title('sin(x)')
   106 
   130 
   107 Here we save the plot of figure(1). 
   131 Here we save the plot of figure(1). 
   108 In []: savefig('sine.png')
   132 In []: savefig('sine.png')
   109 
   133 
   110 close() closes figure(1). Now there is just one figure that is open and hence 
   134 close() closes figure(1). Now there is just one figure that is open and hence 
   112 In []: close()
   136 In []: close()
   113 
   137 
   114 close() now closes the figure(2).
   138 close() now closes the figure(2).
   115 In []: close()
   139 In []: close()
   116 
   140 
   117 The plot command takes the following optional parameters such as 'r' which generates the plot in red color. 
   141 The plot command takes the following optional parameters such as 'g' which generates the plot in green color. 
   118 Use up arrow key to get till this command
       
   119 In []: plot(x, cos(x), 'r') and add argument.
       
   120 
       
   121 In []: clf()
       
   122 
   142 
   123 Passing the linewidth=2 option to plot, generates the plot with linewidth of two units.
   143 Passing the linewidth=2 option to plot, generates the plot with linewidth of two units.
   124 In []: plot(x, sin(x), 'g', linewidth=2)
   144 Use Up arrow to get to the previous commands 
       
   145 In []: plot(x, sin(x), 'g', linewidth=2) and add arguments
   125 
   146 
   126 In []: clf()
   147 In []: clf()
   127 
   148 
   128 In order to plot points you may pass '.' as a parameter to plot
   149 In order to plot points you may pass '.' as a parameter to plot
   129 In []: plot(x, sin(x), '.')
   150 In []: plot(x, sin(x), '.')
   143 In []: clf()
   164 In []: clf()
   144 
   165 
   145 and finally to close the plot
   166 and finally to close the plot
   146 In []: close()
   167 In []: close()
   147 
   168 
       
   169 
       
   170 
       
   171 
   148 ****************
   172 ****************
   149 This brings us to the end of this tutorial.  This tutorial is first in the series of Python for Scientific Computing Tutorials.
   173 This brings us to the end of this tutorial.  Thank you for attending this first tutorial on Python for Scientific Computing 
   150 ****************
   174 ****************