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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Python/C2/Saving-Plots/English''' '''Title of script''': '''Saving-Plots''' '''Author: Chandrika, Pravin, Vineeta Parmar, Thirumalesh H S''' '''Keywords: IPython, savef...")
 
Line 89: Line 89:
 
| 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
  
 
Type,
 
Type,
  
'''x = linspace(-3*pi, 3*pi, 100) '''in the console.
+
'''x = linspace(-3*pi comma 3*pi comma 100) '''in the console.
  
  
Line 105: Line 106:
  
  
| 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, 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.
Line 147: Line 148:
  
  
It which determines the format in which you want to save.
+
It determines the format in which you want to save.
  
 
|-
 
|-
Line 172: Line 173:
  
  
<nowiki>## WILL CHANGE FILE PATHS </nowiki>WHILE RECORDING ##
+
Type '''savefig('slash home slash fossee slash sine.png')'''
 
+
 
+
Type '''savefig('/home/fossee/sine.png')'''
+
  
  
Line 275: Line 273:
 
Save the '''sine''' '''plot''' in the '''eps''' '''format'''.  
 
Save the '''sine''' '''plot''' in the '''eps''' '''format'''.  
  
 
+
Let us see the output for this exercise
 
+
  
 
|-
 
|-
Line 285: Line 282:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Type '''savefig('/home/fossee/sine.eps')'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Type '''savefig('slash home slash fossee slash sine.eps')'''
  
 
and press '''Enter.'''
 
and press '''Enter.'''

Revision as of 13:21, 15 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 %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 = linspace(-3*pi comma 3*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] As 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 different directory,


type full path of the directory before the filename.


Type savefig('slash home slash fossee slash sine.png')



Note that above file path is for Linux based file systems.


For windows based systems, type corresponding path.


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'



[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 /home/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 '/' (on GNU/Linux, Unix based systems), 'C:\' (on windows)
  • Will result in an error as full path is not specified.
  • The current working directory.
  • Predefined directory like “/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 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

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 _________ from IIT Bombay (or FOSSEE, if you wish) signing off.

Thank you.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Vineeta