Python/C2/Saving-plots/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

containing title, name of the production team along with the logo of MHRD

Hello and welcome to the tutorial on "Saving plots".
Show Slide 2

Learning objectives

At the end of this tutorial, you will be able to,
  1. Save plots using savefig() function.
  2. Save plots in different formats.


Show Slide 3

Pre-requisite

Shift to terminal and start ipython -pylab

ipython -pylab
Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively".

Start your IPython interpreter with the command ipython -pylab <Pause>

As you know, it will start your IPython interpreter with the required python modules for plotting and saving your plots.

Show Slide 4

'Creating a basic plot'

To start with, let us plot a sine wave from minus 3 pi to 3 pi. Let us start by calculating the required points for the plot. It can be done using linspace as,
x = linspace(-3*pi,3*pi,100) We have stored the required points in x. Now let us plot the points using the plot statement.
plot(x,sin(x))

Keep the plot open

Done! we have made a very basic sine plot, now let us see how to save the plot for future use so that you can embed the plot in your reports.
Show Slide 5

savefig()

For saving the plot, we will use savefig() function. For this we shall keep the plot window open alongside the terminal. The statement is,
Switch the focus to IPython interpreter window
savefig('/home/fossee/sine.png')
Notice that savefig function takes one argument which is the filename. The last 3 characters after the . in the filename is the extension or type of the file which determines the format in which you want to save.
Highlight the /home/fossee part using mouse movements Also, note that we gave the full path or the absolute path to which we want to save the file.
Highlight the .png part using mouse movements Here we have used an extension .png which means we want to save the image as a PNG file.

Now let us locate the file sine.png which we had saved a while ago. We have saved the file to /home/fossee so let us navigate to /home/fossee using thefile browser.

Open the browser, navigate to /home/fossee and highlight the file sine.png Yes, the file sine.png is here. Let us open it and check.
Open the file sine.png and show it for two-three seconds and then close it and return to IPython interpreter, make sure the plot window is still open, also don't close the file browser window

switch to next slide, More on savefig()

So in-order to save a plot, we use savefig function. savefig can save the plot in many formats, such as pdf - portable document format, ps - post script, eps - encapsulated post script, svg - scalable vector graphics, png - portable network graphics which support transparency etc.
Show Slide 6

Assignment 1

Pause the video here, try out the following exercise and resume the video.

Save the sine plot in the EPS format which can be embedded in LaTeX documents.

Switch focus to the already open plot window We still have the sine plot with us,let us now save the plot as sine.eps.
Switch focus to IPython interpreter Now, We will save the plot using the function savefig
savefig('/home/fossee/sine.eps')

Switch focus to file browser window

Now let us go to /home/fossee and see the new file created.
Highlight the file sine.eps for 2 seconds and

then double click and open the file

Yes! the new file sine.eps is here.
Show Slide 7

Assignment 2

Pause the video here, try out the following exercise and resume the video.

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

Show Slide 8

Summary slide

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 - eps


Show Slide 9

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. Which command is used to save a plot.
    • saveplot()
    • savefig()
    • savefigure()
    • saveplt()
  1. savefig('sine.png') saves the plot in,
    • The root directory / (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 /documents.


Show Slide 10

Solution of self assessment questions on slide

And the answers,
  1. To save a plot,we use the savefig() function.
  2. Whenever we save a file,it gets saved in the current working directory.


Show Slide 11

Acknowledgment slide

Hope you have enjoyed and found it useful. Thank you!

Contributors and Content Editors

Chandrika, Pravin1389