Hello and 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. Before beginning this tutorial,we would suggest you to complete the tutorial on “Using plot interactively”. Now, Start your IPython interpreter with the command ipython hyphen pylab As you know, it will start your IPython interpreter with the required python modules for plotting and saving your plots. 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, You can Type x=linspace within brackets minus 3 into pi comma 3 into pi comma 100 We have stored the required points in x. Now let us plot the points using the plot statement.So type plot (x,sinx) 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. So saving the plot, we will use savefig() function. For this we shall keep the plot window open alongside the terminal. The statement is savefig within brackets in single quotes slash home slash fossee slash sine dot png 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. Also, note that we gave the full path or the absolute path to which we want to save the file. Here we have used an extension dot png which means we want to save the image as a PNG file. Now let us locate the file sine dot png which we had saved a while ago. We have saved the file to slash home slash fossee so let us navigate to slash home slash fossee using thefile browser. Yes, the file sine dot png is here. Let us open it and check So in-order to save a plot , we use savefig function. 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. 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. We still have the sine plot with us,let us now save the plot as sine dot eps. 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 Now let us go to slash home slash fossee and see the new file created. Yes! the new file sine dot epsis 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, Save plots using the savefig() function. then Save the plots in different formats like – pdf – ps – png – svg – eps Here are some self assessment 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 (on GNU/Linux, Unix based systems), c colon slash (on windows). The second option. Will result in an error as full path is not supplied. The third one.The current working directory. and final option is Predefined directory like /documents. And now, 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. Hope you have enjoyed and found it useful.THanks