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