Difference between revisions of "Python-3.4.3/C2/Saving-plots/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(3 intermediate revisions by 2 users not shown)
Line 23: Line 23:
 
Objectives  
 
Objectives  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| At the end of this tutorial, you will be able to -
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| At the end of this tutorial, you will be able to -
 
+
# Save plots using '''savefig() function'''.
 
+
# Save plots using '''savefig()''' function.
+
 
# Save plots in different formats.
 
# Save plots in different formats.
 
 
  
 
|-
 
|-
Line 39: Line 35:
 
* '''Python 3.4.3'''
 
* '''Python 3.4.3'''
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 46: Line 40:
  
 
Pre-requisites
 
Pre-requisites
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To practise this tutorial, you should know how to -
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To practise this tutorial, you should know how to use '''Plot Command interactively.
 
+
'''Use Plot Command Interactively'''
+
  
  
Line 69: Line 61:
  
 
'''%pylab '''and press '''Enter.'''
 
'''%pylab '''and press '''Enter.'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Let us initialise the pylab package
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Let us initialise the '''pylab''' package.
  
  
Type '''%pylab '''and press''' Enter'''
+
Type '''percentage pylab '''and press''' Enter'''
  
 
|-
 
|-
Line 79: Line 71:
 
''''Creating a basic plot''''
 
''''Creating a basic plot''''
  
 
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Let us plot a '''sine curve''' from''' minus 3 pi''' to '''3 pi. '''
 
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Let us plot a '''sine''' '''curve''' from''' minus''''' '''''3 pi to''''' '''''3 pi. '''
+
  
 
|-
 
|-
Line 89: Line 79:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| First, we will calculate the required points for the plot.  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| First, we will calculate the required points for the plot.  
  
To do this
+
To do this, type,
 
+
Type,
+
  
'''x = linspace(-3*pi comma 3*pi comma 100) '''in the console.
+
'''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'''
+
Let us '''plot''' a '''sine curve''' for the points stored in '''variable x'''
  
 
|-
 
|-
Line 106: Line 94:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Type '''plot(x comma sin(x)) '''in the console and press '''Enter. '''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Type '''plot(x comma sin(x)) '''in the '''console''' and press '''Enter. '''
  
Don't close the '''plot''' '''window''' for rest of the tutorial.
+
Don't close the '''plot window''' for rest of the tutorial.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Plot Window]
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Plot Window]
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| As you can see we have made a very basic '''sine plot'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Here you can see we have made a very basic '''sine plot'''.  
  
  
Now let us see how to '''save''' the '''plot.'''
+
Now let us see how to save the '''plot.'''
  
 
|-
 
|-
Line 124: Line 112:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| For saving the plot, we will use the '''savefig()''' function.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| For saving the plot, we will use the '''savefig() function.'''  
  
 
Syntax: '''savefig(fname)'''
 
Syntax: '''savefig(fname)'''
  
  
'''savefig''' function takes one argument which is the filename.  
+
'''savefig function''' takes one '''argument''' which is the filename.  
  
 
|-
 
|-
Line 142: Line 130:
  
  
This saves the file in present working directory.
+
This saves the file in '''present working directory'''.
  
  
The characters after the '''dot '''in the filename is the extension.
+
The characters after the '''dot '''in the filename is the '''extension'''.
  
  
Line 167: Line 155:
  
 
Highlight the '''/home/fossee/''' using mouse cursor
 
Highlight the '''/home/fossee/''' using mouse cursor
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To save the file in different directory,  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To save the file in a different directory, type full path of the directory before the filename.
 
+
 
+
type full path of the directory before the filename.
+
  
  
 
Type '''savefig('slash home slash fossee slash sine.png')'''
 
Type '''savefig('slash home slash fossee slash sine.png')'''
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show slide
  
 +
Path for windows System
  
 
+
''''C:\\Users\\fossee\\Desktop\\sine.png' '''
|-
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"|
+
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Note that above''' file path '''is for''' Linux based file systems.'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Note that above''' file path '''is for''' Linux based file systems.'''
  
  
For''' windows based systems, '''type corresponding''' path.'''
+
For''' Windows, '''give the entire file path as shown here.
 
+
 
+
Example:''' '''if the''' username '''in''' windows '''is''' fossee,'''
+
 
+
and '''sine.png '''is to be saved on '''Desktop'''
+
 
+
 
+
Then filepath is -
+
 
+
''''C:\\Users\\fossee\\Desktop\\sine.png' '''
+
 
+
  
  
 +
Here '''fossee''' is the '''username '''in''' Windows''' and '''sine.png '''file is to be saved on '''Desktop'''.
  
 
|-
 
|-
Line 203: Line 179:
  
 
Highlight the '''.png''' part using mouse movements
 
Highlight the '''.png''' part using mouse movements
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Here we have used an extension '''dot png.'''  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Here we have used an '''extension dot png.'''  
  
 
This will ensure that the image is saved as a '''PNG''' file.
 
This will ensure that the image is saved as a '''PNG''' file.
Line 218: Line 194:
  
 
and highlight the file '''sine.png'''
 
and highlight the file '''sine.png'''
 
 
  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| We have saved the file to  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| We have saved the file to  
Line 226: Line 200:
  
  
Let us navigate to '''(slash)home(slash)fossee''''' ''using the file browser.  
+
Let us navigate to '''(slash)home(slash)fossee''' using the file browser.  
  
 
|-
 
|-
Line 271: Line 245:
  
  
Save the '''sine''' '''plot''' in the '''eps''' '''format'''.  
+
Save the '''sine plot''' in the '''eps format'''.  
  
Let us see the output for this exercise
+
Let us see the output for this exercise.
  
 
|-
 
|-
Line 279: Line 253:
  
 
'''savefig('/home/fossee/sine.eps')'''
 
'''savefig('/home/fossee/sine.eps')'''
 
  
  
Line 285: Line 258:
  
 
and press '''Enter.'''
 
and press '''Enter.'''
 
 
 
  
 
|-
 
|-
Line 294: Line 264:
 
Highlight the file '''sine.eps''' for 2 seconds and
 
Highlight the file '''sine.eps''' for 2 seconds and
  
then double click and open the file
+
then double click and open the file.
 
+
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now let us go to '''/home/fossee''' and see the new file created.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now let us go to '''slash home slash fossee''' and see the new file created.
  
  
Line 316: Line 285:
  
  
Save the '''sine''' '''plot''' in  
+
Save the '''sine plot''' in '''PDF, PS''' and '''SVG formats'''.
 
+
'''PDF, PS''' and '''SVG formats'''.
+
  
 
|-
 
|-
Line 331: Line 298:
 
# Save plots using the '''savefig() '''function.
 
# Save plots using the '''savefig() '''function.
 
# Save the plots in different formats like
 
# Save the plots in different formats like
 
+
#*''' pdf  - ps  - png  - svg  - eps'''
-''' pdf  - ps  - png  - svg  - eps'''
+
  
 
|-
 
|-
Line 341: Line 307:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Here are some assignment questions for you to solve
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Here are some assignment questions for you to solve.
 
+
 
+
# 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), '<u>C</u>:\' (on windows)'''
+
* Will result in an '''error''' as '''full''' '''path''' is not specified.
+
* The current working directory.
+
* Predefined directory like “/documents.”
+
 
+
  
 +
# Which command is used to save a '''plot'''?
 +
#* '''saveplot()'''
 +
#* '''savefig()'''
 +
#* '''savefigure()'''
 +
#* '''saveplt()'''
 +
# '''savefig('sine.png')'''saves the '''plot''' in
 +
#* '''The root directory 'slash' (on GNU/Linux, Unix based systems), '<u>C</u>:' (on windows)'''
 +
#* Will result in an '''error''' as '''full path''' is not supplied.
 +
#* The current '''working directory'''.
 +
#* Predefined directory like “'''slash '''documents.”
  
 
|-
 
|-
Line 364: Line 324:
  
 
Solutions  
 
Solutions  
 
  
  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| And the answers,
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| And the answers,
  
 
+
# To save a plot, we use the '''savefig() function.'''
# To save a plot, we use the '''savefig()''' function.
+
# If we save a file without complete path,  it gets saved in the current '''working directory'''.
# If we save a file without complete path,  it gets saved in the current working directory.
+
 
+
 
+
  
 
|-
 
|-
Line 379: Line 335:
  
 
Forum
 
Forum
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Please post your timed queries in this forum.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Please post your timed questions in this forum.
  
 
|-
 
|-
Line 405: Line 361:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Previous slide
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Previous slide
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| This is _________ from IIT Bombay (or FOSSEE, if you wish) signing off.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| This is Prabhu from IIT Bombay (or FOSSEE, if you wish) signing off.
  
 
Thank you.
 
Thank you.
  
 
|}
 
|}

Latest revision as of 14:56, 28 February 2017

Python/C2/Saving-Plots/English


Title of script: Saving-Plots

Author: Chandrika, Pravin, Vineeta Parmar, Thirumalesh H S

Keywords: IPython, savefig

Visual Cue
Narration
Show Slide

Saving Plots

Hello Friends. Welcome to the tutorial on "Saving Plots".
Show Slide


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

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux 14.04 operating system
  • Python 3.4.3
  • IPython 5.1.0
Show Slide

Pre-requisites

To practise this tutorial, you should know how to use Plot Command interactively.


If not, see the prerequisite Python tutorials on this website.

[Terminal]

ipython3


Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.


Now, type ipython3 and press Enter.

[IPython console]

%pylab and press Enter.

Let us initialise the pylab package.


Type percentage pylab and press Enter

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.


Let us plot a sine curve for the points stored in variable x

[IPython Terminal]


plot(x, sin(x))


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] Here you can see we have made a very basic sine plot.


Now let us see how to save the plot.

Show Slide

savefig()


For saving the plot, we will use the savefig() function.

Syntax: savefig(fname)


savefig function takes one argument which is the filename.

[IPython Terminal]


savefig('sine.png')


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.

[IPython Terminal]

Type pwd and press Enter


To check current working directory,


Type pwd in the console and press Enter

[IPython Terminal]


Highlight the /home/fossee/ using mouse cursor

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')

Show slide

Path for windows System

'C:\\Users\\fossee\\Desktop\\sine.png'

Note that 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.

[IPython Terminal]


Highlight the .png part using mouse movements

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.

[File browser]


Open the browser, navigate to /home/fossee


and highlight the file sine.png

We have saved the file to

(slash)home(slash)fossee,


Let us navigate to (slash)home(slash)fossee using the file browser.

[File browser]


Open the file sine.png and


show it for two-three seconds and then close it.


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


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

Show Slide

Exercise 1


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.

[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.


<pause>


We see the file sine.eps here.

Show Slide

Exercise 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

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

Assignment


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.”
Show Slide

Solutions


And the answers,
  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.
Show Slide

Forum

Please post your timed questions 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

http://spoken-tutorial.org

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Previous slide This is Prabhu from IIT Bombay (or FOSSEE, if you wish) signing off.

Thank you.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Vineeta