saving_plots/script.rst
changeset 522 d33698326409
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
       
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. At the end of this tutorial, you will be able to 
       
     5 
       
     6 .. 1. Save plots using ``savefig()`` function.
       
     7 .. #. Save plots in different formats.
       
     8 
       
     9 
       
    10 .. Prerequisites
       
    11 .. -------------
       
    12 
       
    13 ..   1. should have ``ipython`` and ``pylab`` installed. 
       
    14 ..   #. getting started with ``ipython``.
       
    15 ..   #. using plot command interactively.
       
    16      
       
    17 .. Author              : Anoop Jacob Thomas <anoop@fossee.in>
       
    18    Internal Reviewer   : Puneeth
       
    19    External Reviewer   :
       
    20    Language Reviewer   : Bhanukiran
       
    21    Checklist OK?       : <10-11-2010, Anand, OK> [2010-10-05]
       
    22 
       
    23 .. #[Puneeth: Quickref missing.]
       
    24 
       
    25 =======
       
    26 Savefig
       
    27 =======
       
    28 
       
    29 {{{ Show the first slide }}}
       
    30 
       
    31 Hello and welcome to the tutorial saving plots.
       
    32 
       
    33 {{{ switch to next slide, outline slide }}}
       
    34 
       
    35 In this tutorial you will learn how to save plots using Python, saving
       
    36 in different formats, and locating the file in the file system.
       
    37 
       
    38 .. #[Puneeth: file-system is too technical.]
       
    39 
       
    40 {{{ switch to next slide, a sine wave}}}
       
    41 
       
    42 Start your IPython interpreter with the command ::
       
    43 
       
    44   ipython -pylab
       
    45 
       
    46 As you know, it will start your IPython interpreter with the required
       
    47 python modules for plotting and saving your plots.
       
    48 
       
    49 {{{ Open ipython }}}
       
    50 
       
    51 Now let us plot something, let us plot a sine wave from minus 3 pi to
       
    52 3 pi. Let us start by calculating the required points for the plot. It
       
    53 can be done using linspace as, ::
       
    54 
       
    55   x = linspace(-3*pi,3*pi,100)
       
    56 
       
    57 We have stored required points in x. Now let us plot the points using
       
    58 the statement ::
       
    59 
       
    60   plot(x,sin(x))
       
    61 
       
    62 {{{ Keep the plot open }}}
       
    63 
       
    64 Done! we have made a very basic sine plot, now let us see how to save
       
    65 the plot for future use so that you can embed the plot in your
       
    66 reports.
       
    67 
       
    68 .. #[Puneeth: All this is known stuff. You don't have to elaborate so
       
    69 .. much on it. Just say, let us plot sin function from -3 pi to 3
       
    70 .. pi. Show the commands, and be done with it. ]
       
    71 
       
    72 {{{ switch to next slide, savefig() }}}
       
    73 
       
    74 {{{ Switch the focus to IPython interpreter window }}}
       
    75 
       
    76 For saving the plot, we will use ``savefig()`` function, and it has to be
       
    77 done with the plot window open. The statement is, ::
       
    78 
       
    79   savefig('/home/fossee/sine.png')
       
    80 
       
    81 Notice that ``savefig`` function takes one argument which is the
       
    82 filename. The last 3 characters after the ``.`` in the filename is the
       
    83 extension or type of the file which determines the format in which you
       
    84 want to save.
       
    85 
       
    86 .. #[Puneeth: removed mention of string]
       
    87 
       
    88 .. #[[Anoop: I think this treatment is better :) ]]
       
    89 
       
    90 {{{ Highlight the /home/fossee part using mouse movements }}}
       
    91 
       
    92 Also, note that we gave the full path or the absolute path to which we
       
    93 want to save the file.
       
    94 
       
    95 {{{ Highlight the .png part using mouse movements }}}
       
    96 
       
    97 Here I have used an extension ``.png`` which means I want to save the
       
    98 image as a PNG file.
       
    99 
       
   100 Now let us locate ``sine.png`` file saved. We saved the file to
       
   101 ``/home/fossee`` so let us navigate to ``/home/fossee`` using the
       
   102 file browser.
       
   103 
       
   104 {{{ Open the browser, navigate to /home/fossee and highlight the file
       
   105 sine.png }}}
       
   106 
       
   107 Yes, the file ``sine.png`` is here and let us check it.
       
   108 
       
   109 {{{ Open the file sine.png and show it for two-three seconds and then
       
   110 close it and return to IPython interpreter, make sure the plot window
       
   111 is still open, also don't close the file browser window }}}
       
   112 
       
   113 {{{ switch to next slide, More on savefig() }}}
       
   114 
       
   115 So in-order to save a plot, we use ``savefig`` function. ``savefig``
       
   116 can save the plot in many formats, such as pdf - portable document
       
   117 format, ps - post script, eps - encapsulated post script, svg -
       
   118 scalable vector graphics, png - portable network graphics which
       
   119 support transparency etc.
       
   120 
       
   121 .. #[[slide must give the extensions for the files - Anoop]]
       
   122 
       
   123 {{{ switch to next slide, exercise 1 }}}
       
   124 
       
   125 Let us now try to save the plot in eps format. ``eps`` stands for
       
   126 encapsulated post script, and it can be embedded in your latex
       
   127 documents. Pause here and try to figure it out yourself.
       
   128 
       
   129 {{{ Switch focus to the already open plot window }}}
       
   130 
       
   131 We still have the sine plot with us, and now let us save the plot as
       
   132 ``sine.eps``.
       
   133 
       
   134 {{{ switch to next slide, solution 1 }}}
       
   135 
       
   136 {{{ Switch focus to IPython interpreter }}}
       
   137 
       
   138 Now, We will save the plot using the function ``savefig`` ::
       
   139 
       
   140   savefig('/home/fossee/sine.eps')
       
   141 
       
   142 {{{ Switch focus to file browser window }}}
       
   143 
       
   144 Now let us go to ``/home/fossee`` and see the new file created.
       
   145 
       
   146 {{{ Highlight the file sine.eps with a single mouse click for 2
       
   147 seconds and then double click and open the file }}}
       
   148 
       
   149 Yes! the new file ``sine.eps`` is here.
       
   150 
       
   151 {{{ switch to next slide, exercise 2 }}}
       
   152 
       
   153 Now you may try saving the same in pdf, ps, svg formats.
       
   154 
       
   155 {{{ Switch to summary slide }}}
       
   156 
       
   157 This brings us to the end of this tutorial, in this tutorial we
       
   158 learned to save plots using the function ``savefig()``. Saving the
       
   159 plots in different formats and locating the files in the file system.
       
   160 
       
   161 {{{ switch to Thank you slide }}}
       
   162 
       
   163 Thank you!