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