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