savefig/script.rst
changeset 261 c7f0069d698a
child 290 290f3e62dc44
equal deleted inserted replaced
260:25b4e962b55e 261:c7f0069d698a
       
     1 .. 2.5 LO: saving plots (2) 
       
     2 .. -------------------------
       
     3 .. * Outline 
       
     4 ..   + basic savefig 
       
     5 ..   + png, pdf, ps, eps, svg 
       
     6 ..   + going to OS and looking at the file 
       
     7 
       
     8 =======
       
     9 Savefig
       
    10 =======
       
    11 
       
    12 Hello and welcome to the tutorial. In this tutorial you will learn how
       
    13 to save plots using Python.
       
    14 
       
    15 Start your IPython interpreter with the command ::
       
    16 
       
    17   ipython -pylab
       
    18 
       
    19 It will start your IPython interpreter with the required python
       
    20 modules for plotting and saving your plots.
       
    21 
       
    22 {{{ Open ipython }}}
       
    23 
       
    24 Now let us plot something, let us plot a sine wave from minus 3 pi to
       
    25 3 pi. Let us start by calculating the required points for the plot. It
       
    26 can be done using linspace as, ::
       
    27 
       
    28   x = linspace(-3*pi,3*pi,100)
       
    29 
       
    30 We have stored required points in x. Now let us plot the points using
       
    31 the statement ::
       
    32 
       
    33   plot(x,sin(x))
       
    34 
       
    35 {{{ Keep the plot open }}}
       
    36 
       
    37 Done! we have made a very basic sine plot, now let us see how to save
       
    38 the plot for future use so that you can embed the plot in your
       
    39 reports.
       
    40 
       
    41 {{{ Switch the focus to IPython interpreter window }}}
       
    42 
       
    43 For saving the plot, we will use savefig function, and it has to be
       
    44 done with the plot window open. The statement is, ::
       
    45 
       
    46   savefig('/home/fossee/sine.png')
       
    47 
       
    48 Notice that ``savefig`` function takes one argument which is a string
       
    49 which is the filename. The last 3 characters after the ``.`` in the
       
    50 filename is the extension or type of the file which determines the
       
    51 format in which you want to save.
       
    52 
       
    53 {{{ Highlight the /home/fossee part using mouse movements }}}
       
    54 
       
    55 Also, note that we gave the full path or the absolute path to which we
       
    56 want to save the file.
       
    57 
       
    58 {{{ Highlight the .png part using mouse movements }}}
       
    59 
       
    60 Here I have used an extension ``.png`` which means i want to save the
       
    61 image as a PNG file.
       
    62 
       
    63 Now let us locate ``sine.png`` file saved. We saved the file to
       
    64 ``/home/fossee`` so let us navigate to ``/home/fossee`` using the
       
    65 file browser.
       
    66 
       
    67 {{{ Open the browser, navigate to /home/fossee and highlight the file
       
    68 sine.png }}}
       
    69 
       
    70 Yes, the file ``sine.png`` is here and let us check it.
       
    71 
       
    72 {{{ Open the file sine.png and show it for two-three seconds and then
       
    73 close it and return to IPython interpreter, make sure the plot window
       
    74 is still open, also don't close the file browser window }}}
       
    75 
       
    76 So in-order to save a plot, we use ``savefig`` function. ``savefig``
       
    77 can save the plot in many formats, such as pdf - portable document
       
    78 format, ps - post script, eps - encapsulated post script, svg -
       
    79 scalable vector graphics, png - portable network graphics which
       
    80 support transparency etc.
       
    81 
       
    82 .. #[[slide must give the extensions for the files - Anoop]]
       
    83 
       
    84 Let us now try to save the plot in eps format. ``eps`` stands for
       
    85 encapsulated post script, and it can be embedded in your latex
       
    86 documents.
       
    87 
       
    88 {{{ Switch focus to the already open plot window }}}
       
    89 
       
    90 We still have the sine plot with us, and now let us save the plot as
       
    91 ``sine.eps``.
       
    92 
       
    93 {{{ Switch focus to IPython interpreter }}}
       
    94 
       
    95 Now, We will save the plot using the function ``savefig`` ::
       
    96 
       
    97   savefig('/home/fossee/sine.eps')
       
    98 
       
    99 {{{ Switch focus to file browser window }}}
       
   100 
       
   101 Now let us go to ``/home/fossee`` and see the new file created.
       
   102 
       
   103 {{{ Highlight the file sine.eps with a single mouse click for 2
       
   104 seconds and then double click and open the file }}}
       
   105 
       
   106 Yes! the new file ``sine.eps`` is here.
       
   107 
       
   108 Now you may try saving the same in pdf, ps, svg formats.
       
   109 
       
   110 Let us review what we have learned in this session! We have learned to
       
   111 save plots in different formats using the function ``savefig()``.
       
   112 
       
   113 Thank you!
       
   114 
       
   115 ..  Author: Anoop Jacob Thomas <anoop@fossee.in>
       
   116     Reviewer 1:
       
   117     Reviewer 2:
       
   118     External reviewer: