Python/C2/Saving-plots/English-timed
From Script | Spoken-Tutorial
Time | Narration |
00:00 | Hello and welcome to the tutorial on "Saving plots". |
00:04 | At the end of this tutorial, you will be able to-
Save plots using savefig() function. Save plots in different formats. |
00:13 | Before beginning this tutorial, we would suggest you to complete the tutorial on "Using plot interactively". |
00:19 | Now, start your IPython interpreter with the command ipython hyphen pylab. |
00:30 | As you know, it will start your IPython interpreter with the required python modules for plotting and saving your plots. |
00:36 | To start with, let us plot a sine wave from minus 3 pi to 3 pi. |
00:43 | Let us start by calculating the required points for the plot. |
00:45 | It can be done using linspace as- |
00:51 | You can type: x = linspace within brackets minus 3 into pi comma 3 into pi comma 100. |
00:59 | We have stored the required points in 'x'. |
01:03 | Now, let us plot the points using the 'plot' statement. So, type: plot (x, sinx). |
01:18 | 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. |
01:32 | So, saving the plot, we will use savefig() function. |
01:36 | For this, we shall keep the plot window open alongside the terminal. |
01:40 | The statement is: savefig within brackets in single quotes slash home slash fossee slash sine dot png. |
01:52 | Notice that savefig() function takes one argument which is the filename. The last 3 characters after the '.' in the filename, is the extension and type of the file which determines the format in which you want to save. |
02:10 | Also, note that we gave the full path or the absolute path to which we want to save the file. |
02:18 | Here, we have used an extension dot png which means we want to save the image as a PNG file. |
02:25 | Now, let us locate the file sine dot png which we had saved a while ago. |
02:32 | We have saved the file to slash home slash fossee. So, let us navigate to slash home slash fossee using the file browser. |
02:49 | Yes, the file sine dot png is here. |
02:52 | Let us open it and check. |
02:57 | So, in-order to save a plot, we use savefig() function. |
03:05 | dot 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. |
03:24 | Pause the video here, try out the following exercise and resume the video. |
03:29 | Save the sine plot in the EPS format which can be embedded in LaTeX documents. |
03:37 | We still have the sine plot with us, let us now save the plot as sine dot eps. |
03:49 | Now, we will save the plot using the function savefig(). So, you can type: savefig within brackets in single quotes slash home slash fossee slash sine dot eps and hit Enter. |
04:04 | Now, let us go to: slash home slash fossee and see the new file created. |
04:13 | Yes! the new file sine dot eps is here. |
04:18 | Pause the video here, try out the following exercise and resume the video. |
04:23 | Save the sine plot in PDF, PS and SVG formats. |
04:31 | This brings us to the end of this tutorial. |
04:34 | In this tutorial, we have learnt to save plots using the savefig() function. |
04:38 | Then save the plots in different formats like - pdf, ps, png, svg, eps. |
04:45 | Here are some self assessment questions for you to solve- |
04:48 | 1. Which command is used to save a plot. saveplot(), savefig(), savefigure(), saveplt()? |
04:59 | 2. savefig('sine.png') saves the plot in- |
05:04 | The root directory (on GNU/Linux, Unix based systems)- c colon slash (on windows). |
05:14 | The second option- Will result in an error as full path is not supplied. |
05:18 | The third one- The current working directory. |
05:21 | and final option is- Predefined directory like "/documents". |
05:26 | And now, the answers- |
05:28 | 1. To save a plot, we use the savefig() function. |
05:33 | 2. Whenever we save a file, it gets saved in the current working directory. |
05:38 | Hope you have enjoyed and found it useful. Thanks. |
Contributors and Content Editors
Gaurav, Jyotisolanki, Minal, PoojaMoolya, Sandhya.np14, Sneha