Python-3.4.3/C2/Saving-plots/English
Python/C2/Saving-Plots/English
Title of script: Saving-Plots
Author: Chandrika, Pravin, Vineeta Parmar, Thirumalesh H S
Keywords: IPython, savefig
|
|
Show Slide
“Saving Plots” |
Hello Friends. Welcome to the tutorial on "Saving Plots". |
Show Slide
|
At the end of this tutorial, you will be able to -
|
Show Slide
System Requirements |
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
To practise this tutorial, you should know how to -
Use Plot Command Interactively
|
[Terminal]
ipython3
|
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.
|
[IPython console]
%pylab and press Enter. |
Let us initialise the pylab package
|
Show Slide
'Creating a basic plot'
|
Let us plot a sine curve from minus 3 pi to 3 pi. |
[IPython Terminal]
x = linspace(-3*pi, 3*pi, 100) |
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.
|
[IPython Terminal]
|
Type plot(x comma sin(x)) in the console and press Enter.
Don't close the plot window for rest of the tutorial. |
[Plot Window] | As you can see we have made a very basic sine plot.
|
Show Slide
savefig()
|
For saving the plot, we will use the savefig() function.
Syntax: savefig(fname)
|
[IPython Terminal]
|
Type savefig('sine.png') and press Enter.
|
[IPython Terminal]
Type pwd and press Enter
|
To check current working directory,
|
[IPython Terminal]
|
To save the file in different directory,
|
Show slide
Path for windows System 'C:\\Users\\fossee\\Desktop\\sine.png' |
Note that above file path is for Linux based file systems.
and sine.png file is to be saved on Desktop
|
[IPython Terminal]
|
Here we have used an extension dot png.
This will ensure that the image is saved as a PNG file.
|
[File browser]
|
We have saved the file to
(slash)home(slash)fossee,
|
[File browser]
|
Open the sine.png file to see the plotted sine curve. |
Show Slide
More on savefig()
|
savefig can save the plot in many formats, such as
ps - post script, eps - encapsulated post script, to be used with LaTeX documents svg - scalable vector graphics, png - portable network graphics |
Show Slide
Exercise 1
|
Pause the video here, try out the following exercise and resume the video.
Let us see the output for this exercise |
[Ipython Terminal]
savefig('/home/fossee/sine.eps')
|
Type savefig('slash home slash fossee slash sine.eps')
and press Enter.
|
[File Browser]
Highlight the file sine.eps for 2 seconds and then double click and open the file
|
Now let us go to slash home slash fossee and see the new file created.
|
Show Slide
Exercise 2
|
Pause the video here, try out the following exercise and resume the video.
PDF, PS and SVG formats. |
Show Slide
Summary slide
|
This brings us to the end of this tutorial. In this tutorial,we have learnt to,
- pdf - ps - png - svg - eps |
Show Slide
Assignment
|
Here are some assignment questions for you to solve
2. savefig('sine.png')saves the plot in,
|
Show Slide
Solutions
|
And the answers,
|
Show Slide
Forum |
Please post your timed queries in this forum. |
Show Slide
Fossee Forum |
Please post your general queries on Python in this forum. |
Show Slide
Textbook Companion |
FOSSEE team coordinates the TBC project. |
Show Slide
Acknowledgment |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is _________ from IIT Bombay (or FOSSEE, if you wish) signing off.
Thank you. |