Python-3.4.3/C2/Saving-plots/English-timed

From Script | Spoken-Tutorial
Revision as of 16:37, 12 June 2019 by Sandhya.np14 (Talk | contribs)

Jump to: navigation, search
Time Narration
00:01 Hello friends! Welcome to the tutorial on "Saving Plots".
00:06 At the end of this tutorial, you will be able to -

save plots using savefig() function,

save plots in different formats.

00:15 To record this tutorial, I am using:

Ubuntu Linux 14.04 operating system,

Python 3.4.3,

IPython 5.1.0

00:28 To practise this tutorial, you should know how to use Plot command interactively.

If not, see the prerequisite Python tutorials on this website.

00:40 Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.

Now, type ipython3 and press Enter.

00:54 Let us initialise the 'pylab' package.

Type percentage pylab and press Enter.

01:05 Let us plot a sine curve from minus 3 pi to 3 pi.
01:10 First, we will calculate the required points for the plot.
01:14 To do this, type:

x equals to linspace(minus 3 star pi comma 3 star pi comma 100) in the console.

01:28 Let us plot a sine curve for the points stored in variable x.
01:33 Type: plot(x comma sin(x)) in the console and press Enter.

Don't close the plot window for rest of the tutorial.

01:47 Here you can see we have made a very basic sine plot. Now let us see how to save the plot.
01:54 For saving the plot, we will use the 'savefig()' function.

Syntax: savefig(fname)

savefig function takes one argument which is the filename.

02:05 Type: savefig('sine.png') and press Enter.
02:12 This saves the file in present working directory.
02:16 The characters after the dot in the filename is the extension. It determines the format in which you want to save the file.
02:27 To check the current working directory, type pwd in the console and press Enter.
02:34 To save the file in a different directory, type full path of the directory before the filename.

Type: savefig('slash home slash fossee slash sine.png')

02:53 Note that the above file path is for Linux based file systems.
02:59 For Windows, give the entire file path as shown here. Here fossee is the username in Windows and sine.png file is to be saved on Desktop.
03:15 Here we have used an extension dot png. This will ensure that the image is saved as a PNG file.
03:24 Now let us locate the file sine.png which we had saved earlier.
03:30 We have saved the file to

(slash)home(slash)fossee.

03:35 Let us navigate to (slash)home(slash)fossee using the file browser.
03:40 Open the sine.png file to see the plotted sine curve.
03:46 savefig can save the plot in many formats such as-

pdf - portable document format,

ps - post script,

03:57 eps - encapsulated post script, to be used with LaTeX documents,

svg - scalable vector graphics,

.png - portable network graphics .

04:10 Pause the video here, try out the following exercise and resume the video.
04:16 Save the sine plot in the eps format. Let us see the output for this exercise.
04:23 Type: savefig('slash home slash fossee slash sine.eps') and press Enter.
04:35 Now let us go to slash home slash fossee and see the new file created. We see the file sine.eps here.
04:48 Pause the video here, try out the following exercise and resume the video.

Save the sine plot in PDF, PS and SVG formats.

05:00 This brings us to the end of this tutorial. In this tutorial, we have learnt to:

1. Save plots using the savefig() function.

2. Save the plots in different formats like pdf, ps, png, svg and eps.

05:17 Here are some assignment questions for you to solve.

1. Which command is used to save a plot? saveplot(),

savefig(),

savefigure(),

saveplt()

2. savefig('sine.png')saves the plot in- the root directory 'slash' (on GNU/Linux, Unix based systems), 'C:' (on windows),

05:40 will result in an error as full path is not supplied,

the current working directory,

predefined directory like “slash documents.”

05:50 And the answers are-

1. To save a plot, we use the savefig() function.

2. If we save a file without complete path, it gets saved in the current working directory.

06:02 Please post your timed questions in this forum.
06:07 Please post your general queries on Python in this forum.
06:12 FOSSEE team coordinates the TBC project.
06:16 Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.
06:25 This is Prabhu from IIT Bombay, signing off. Thank you.

Contributors and Content Editors

Pratik kamble, Sandhya.np14