using-plot-interactively/script.rst
changeset 522 d33698326409
parent 521 88a01948450d
child 523 54bdda4aefa5
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. By the end of this tutorial you will --
       
     5 
       
     6 .. 1. Create simple plots of mathematical functions
       
     7 .. #. Use the Figure window to study plots better
       
     8 
       
     9 
       
    10 
       
    11 .. Prerequisites
       
    12 .. -------------
       
    13 
       
    14 .. Installation of required tools
       
    15 .. Ipython
       
    16      
       
    17 .. Author              : Amit Sethi
       
    18    Internal Reviewer   : 
       
    19    External Reviewer   :
       
    20    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
       
    21 
       
    22 Script
       
    23 -------
       
    24 {{{ Show the Title Slide }}} 
       
    25 
       
    26 Hello and welcome to the tutorial on creating simple plots using
       
    27 Python.This tutorial is presented by the Fossee group.  
       
    28 
       
    29 I hope you have IPython running on your computer.
       
    30 
       
    31 In this tutorial we will look at plot command and also how to study
       
    32 the plot using the UI.
       
    33 
       
    34 {{{ Show Outline Slide }}}
       
    35 
       
    36 Lets start ipython on your shell, type :: 
       
    37 
       
    38       $ipython -pylab
       
    39 
       
    40 
       
    41 Pylab is a python library which provides plotting functionality.It
       
    42 also provides many other important mathematical and scientific
       
    43 functions. After running IPython -pylab in your shell if at the top of
       
    44 the result of this command, you see something like ::
       
    45  
       
    46 
       
    47    `ERROR: matplotlib could NOT be imported!  Starting normal
       
    48       IPython.`
       
    49 
       
    50 
       
    51 {{{ Slide with Error written on it }}}
       
    52 
       
    53 
       
    54 Then you have to install matplotlib and run this command again.
       
    55 
       
    56 Now type in your ipython shell ::
       
    57 
       
    58              linpace?
       
    59 
       
    60 
       
    61 
       
    62 as the documentation says, it returns `num` evenly spaced samples,
       
    63 calculated over the interval start and stop.  To illustrate this, lets
       
    64 do it form 1 to 100 and try 100 points.  ::
       
    65 
       
    66             linspace(1,100,100)
       
    67 
       
    68 As you can see a sequence of numbers from 1 to 100 appears.
       
    69 
       
    70 Now lets try 200 points between 0 and 1 you do this by typing ::
       
    71 
       
    72 
       
    73              linspace(0,1,200)
       
    74 
       
    75 0 for start , 1 for stop and 200 for no of points.  In linspace 
       
    76 the start and stop points can be integers, decimals , or
       
    77 constants. Let's try and get 100 points between -pi to pi. Type ::
       
    78            
       
    79              p = linspace(-pi,pi,100)
       
    80 
       
    81 
       
    82 'pi' here is constant defined by pylab. Save this to the variable, p
       
    83 .
       
    84 
       
    85 If you now ::
       
    86      
       
    87 	    len(p)
       
    88 
       
    89 You will get the no. of points. len function gives the no of elements
       
    90 of a sequence.
       
    91 
       
    92 
       
    93 Let's try and plot a cosine curve between -pi and pi using these
       
    94 points.  Simply type::
       
    95 
       
    96 	 plot(p,cos(points)) 
       
    97 
       
    98 
       
    99 Here cos(points) gets the cosine value at every corresponding point to
       
   100 p.
       
   101 
       
   102 
       
   103 We can also save cos(points) to variable cosine and plot it using
       
   104 plot.::
       
   105 
       
   106           cosine=cos(points) 
       
   107 
       
   108 	  plot(p,cosine)
       
   109 
       
   110  
       
   111 
       
   112 Now do ::
       
   113        	 
       
   114 	  clf()
       
   115 
       
   116 this will clear the plot.
       
   117 
       
   118 This is done because any other plot we try to make shall come on the
       
   119 same drawing area. As we do not wish to clutter the area with
       
   120 overlaid plots , we just clear it with clf().  Now lets try a sine
       
   121 plot. ::
       
   122 
       
   123 
       
   124     	  plot(p,sin(p))
       
   125 
       
   126 
       
   127 
       
   128  
       
   129 The Window on which the plot appears can be used to study it better.
       
   130 
       
   131 {{{ Show the slide with all the buttons on it }}}
       
   132 
       
   133 First of all moving the mouse around gives us the point where mouse
       
   134 points at.  
       
   135 
       
   136 Also we have some buttons the right most among them is
       
   137 for saving the file. 
       
   138 
       
   139 Just click on it specifying the name of the file.  We will save the plot 
       
   140 by the name sin_curve in pdf format.
       
   141 
       
   142 
       
   143 
       
   144 {{{ Show how to save the file  }}}
       
   145 
       
   146 As you can see I can specify format of file from the dropdown.
       
   147 
       
   148 Formats like png ,eps ,pdf, ps are available.
       
   149 
       
   150 Left to the save button is the slider button to specify the margins.
       
   151 
       
   152 {{{ Show how to zoom. Press zoom button and specify region to zoom }}}
       
   153 
       
   154 Left to this is zoom button to zoom into the plot. Just specify the 
       
   155 region to zoom into.  
       
   156 
       
   157 {{{ Press Move button and move the axes. }}}
       
   158 
       
   159 The button left to it can be used to move the axes of the plot.  
       
   160 
       
   161 {{{ Press Back and Forward Button }}}
       
   162  
       
   163 The next two buttons with a left and right arrow icons change the state of the 
       
   164 plot and take it to the previous state it was in. It more or less acts like a
       
   165 back and forward button in the browser.  
       
   166 
       
   167 {{{ Press home button }}}
       
   168 
       
   169 The last one is 'home' referring to the initial plot.
       
   170 
       
   171 
       
   172 
       
   173 
       
   174 Following is an  exercise that you must do. 
       
   175 
       
   176 %% %% Plot (sin(x)*sin(x))/x .
       
   177       1. Save the plot by the sinsquarebyx.pdf in pdf format.
       
   178       2. Zoom and find the maxima.
       
   179 
       
   180       3. Bring it back to initial position.
       
   181 
       
   182 
       
   183 Please, pause the video here. Do the exercise and then continue. 
       
   184 
       
   185 
       
   186 
       
   187 
       
   188 
       
   189 
       
   190 
       
   191 
       
   192 
       
   193 {{{ Summary Slide }}}
       
   194 
       
   195 In this tutorial we have looked at 
       
   196 
       
   197 1. Starting Ipython with pylab 
       
   198 
       
   199 2. Using linspace function to create `num` equaly spaced points in a region.
       
   200 
       
   201 3. Finding length of sequnces using  len.
       
   202  
       
   203 4. Plotting mathematical functions using plot.
       
   204 
       
   205 4. Clearing drawing area using clf 
       
   206  
       
   207 5. Using the UI of plot for studying it better . Using functionalities like save , zoom and moving the plots on x and y axis 
       
   208 
       
   209 
       
   210  {{{ Show the "sponsored by FOSSEE" slide }}}
       
   211 
       
   212  
       
   213 
       
   214 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   215 
       
   216  
       
   217 
       
   218  Hope you have enjoyed and found it useful.
       
   219 
       
   220  Thankyou
       
   221 
       
   222  
       
   223 
       
   224 Author              : Amit Sethi
       
   225 Internal Reviewer   :
       
   226 Internal Reviewer 2 :