Difference between revisions of "Python-3.4.3/C2/Saving-plots/English"
Nancyvarkey (Talk | contribs) |
|||
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()''' | + | |
# 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. |
− | + | ||
− | ''' | + | |
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. |
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 | + | |
|- | |- | ||
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, |
− | + | ||
− | + | ||
− | '''x equals to linspace(minus 3 star pi comma 3 star 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 | + | 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 | + | Don't close the '''plot window''' for rest of the tutorial. |
|- | |- | ||
Line 115: | Line 103: | ||
− | Now let us see how to | + | 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()''' | + | | 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''' | + | '''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')''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 187: | Line 169: | ||
− | For''' | + | 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'''. | ||
|- | |- | ||
Line 202: | 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 | + | | 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 217: | 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 225: | Line 200: | ||
− | Let us navigate to '''(slash)home(slash)fossee | + | Let us navigate to '''(slash)home(slash)fossee''' using the file browser. |
|- | |- | ||
Line 270: | Line 245: | ||
− | Save the '''sine | + | 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 278: | Line 253: | ||
'''savefig('/home/fossee/sine.eps')''' | '''savefig('/home/fossee/sine.eps')''' | ||
− | |||
Line 284: | Line 258: | ||
and press '''Enter.''' | and press '''Enter.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 293: | 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. |
− | + | ||
Line 315: | Line 285: | ||
− | Save the '''sine | + | Save the '''sine plot''' in '''PDF, PS''' and '''SVG formats'''. |
− | + | ||
− | '''PDF, PS''' and '''SVG formats'''. | + | |
|- | |- | ||
Line 330: | 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''' | |
− | + | ||
|- | |- | ||
Line 340: | 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()''' | ||
+ | # '''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 363: | 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()''' | + | # 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 378: | 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 | + | | 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 404: | 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 | + | | 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
|
|
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] | Here 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 a different directory, type full path of the directory before the filename.
|
Show slide
Path for windows System 'C:\\Users\\fossee\\Desktop\\sine.png' |
Note that above file path is for Linux based file systems.
|
[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.
|
Show Slide
Summary slide
|
This brings us to the end of this tutorial. In this tutorial,we have learnt to,
|
Show Slide
Assignment
|
Here are some assignment questions for you to solve.
|
Show Slide
Solutions
|
And the answers,
|
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 |
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. |