Hello friends! Welcome to the tutorial on "Saving Plots".
At the end of this tutorial, you will be able to - save plots using savefig() function, save plots in different formats.
To record this tutorial, I am using: Ubuntu Linux 14.04 operating system, Python 3.4.3, IPython 5.1.0
To practise this tutorial, you should know how to use Plot command interactively. If not, see the prerequisite Python tutorials on this website.
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously. Now, type ipython3 and press Enter.
Let us initialise the 'pylab' package. Type percentage pylab and press Enter.
Let us plot a sine curve from minus 3 pi to 3 pi.
First, we will calculate the required points for the plot.
To do this, type: x equals to linspace(minus 3 star pi comma 3 star pi comma 100) in the console.
Let us plot a sine curve for the points stored in variable x.
Type: plot(x comma sin(x)) in the console and press Enter. Don't close the plot window for rest of the tutorial.
Here you can see we have made a very basic sine plot. Now let us see how to save the plot.
For saving the plot, we will use the 'savefig()' function. Syntax: savefig(fname) savefig function takes one argument which is the filename.
Type: savefig('sine.png') and press Enter.
This saves the file in present working directory.
The characters after the dot in the filename is the extension. It determines the format in which you want to save the file.
To check the current working directory, type pwd in the console and press Enter.
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')
Note that the above file path is for Linux based file systems.
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.
Here we have used an extension dot png. This will ensure that the image is saved as a PNG file.
Now let us locate the file sine.png which we had saved earlier.
We have saved the file to (slash)home(slash)fossee.
Let us navigate to (slash)home(slash)fossee using the file browser.
Open the sine.png file to see the plotted sine curve.
savefig can save the plot in many formats such as- pdf - portable document format, ps - post script,
eps - encapsulated post script, to be used with LaTeX documents, svg - scalable vector graphics, .png - portable network graphics .
Pause the video here, try out the following exercise and resume the video.
Save the sine plot in the eps format. Let us see the output for this exercise.
Type: savefig('slash home slash fossee slash sine.eps') and press Enter.
Now let us go to slash home slash fossee and see the new file created. We see the file sine.eps here.
Pause the video here, try out the following exercise and resume the video. Save the sine plot in PDF, PS and SVG formats.
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.
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),
will result in an error as full path is not supplied, the current working directory, predefined directory like “slash documents.”
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.
Please post your timed questions in this forum.
Please post your general queries on Python in this forum.
FOSSEE team coordinates the TBC project.
Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.
This is Prabhu from IIT Bombay, signing off. Thank you.