Difference between revisions of "Python-3.4.3/C2/Using-plot-command-interactively/English"
(→Python/C2/Creating-simple-plots-using-IPython/English) |
Nancyvarkey (Talk | contribs) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 29: | Line 29: | ||
# Create simple '''plots''' of '''mathematical''' '''functions'''. | # Create simple '''plots''' of '''mathematical''' '''functions'''. | ||
# Use the '''Plot''' '''window''' to study '''plots '''better. | # Use the '''Plot''' '''window''' to study '''plots '''better. | ||
− | |||
− | |||
|- | |- | ||
Line 41: | Line 39: | ||
* '''Python 3.4.3''' | * '''Python 3.4.3''' | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 73: | Line 69: | ||
'''%pylab '''and press '''Enter.''' | '''%pylab '''and press '''Enter.''' | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| Let us initialise the pylab package | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| Let us initialise the '''pylab package'''. |
Line 90: | Line 86: | ||
* which provides '''plotting''' functionality | * which provides '''plotting''' functionality | ||
* and has '''mathematical''' and '''scientific''' '''functions'''. | * and has '''mathematical''' and '''scientific''' '''functions'''. | ||
− | |||
− | |||
|- | |- | ||
Line 97: | Line 91: | ||
Highlight “'''Using matplotlib backend: TkAgg''' ” | Highlight “'''Using matplotlib backend: TkAgg''' ” | ||
− | | style="border:0.5pt solid #000000;padding:0.0417in;"| After running ''' | + | | style="border:0.5pt solid #000000;padding:0.0417in;"| After running '''percentage pylab '''in the i'''Python console''' you will see a message- |
“'''Using matplotlib backend: TkAgg ”.''' | “'''Using matplotlib backend: TkAgg ”.''' | ||
Line 122: | Line 116: | ||
− | Type ''''linspace'''' followed by a question mark in '''ipython console. '''Press Enter | + | Type ''''linspace'''' followed by a question mark in '''ipython console. ''' |
+ | |||
+ | Press '''Enter''' | ||
Line 140: | Line 136: | ||
linspace(1, 100, 100) >> press '''Enter'''. | linspace(1, 100, 100) >> press '''Enter'''. | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Let's try to generate 100 points from 1 to 100.Type '''linspace brackets 1 comma 100 comma 100''' | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Let's try to generate 100 points from 1 to 100. Type '''linspace brackets 1 comma 100 comma 100''' |
Line 146: | Line 142: | ||
− | Now, press '''Enter'''. As you can see, a sequence of numbers from 1 to 100 is displayed. | + | Now, press '''Enter'''. |
+ | |||
+ | As you can see, a sequence of numbers from 1 to 100 is displayed. | ||
|- | |- | ||
Line 174: | Line 172: | ||
− | First we will generate 100 points between ''' | + | First we will generate 100 points between '''minus pi''' and '''pi'''. |
− | So type '''linspace brackets minus pi comma pi comma 100 '''and''' '''press | + | So type '''linspace brackets minus pi comma pi comma 100 '''and''' '''press '''Enter'''. |
Line 186: | Line 184: | ||
'''t = linspace(-pi, pi, 100)''' | '''t = linspace(-pi, pi, 100)''' | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, let us save this to a variable, say '''t'''. Press | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, let us save this to a variable, say '''t'''. |
+ | |||
+ | Press '''Enter'''. | ||
|- | |- | ||
Line 192: | Line 192: | ||
'''len(t)''' | '''len(t)''' | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| If we now type '''len bracket t '''and press '''Enter''', we will get the number of points between '''minus''' '''pi''' and '''pi.len''' function gives the no. of elements present in a given sequence. | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| If we now type '''len bracket t '''and press '''Enter''', we will get the number of points between '''minus''' '''pi''' and '''pi. |
+ | |||
+ | '''len''' function gives the no. of elements present in a given sequence. | ||
|- | |- | ||
Line 198: | Line 200: | ||
'''plot(t, cos(t))''' | '''plot(t, cos(t))''' | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Next, let's try and plot a '''cosine''' '''curve''' between '''minus''' '''pi''' and''' pi.'''For this, we use the '''plot''' command. | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Next, let's try and plot a '''cosine''' '''curve''' between '''minus''' '''pi''' and''' pi.''' |
+ | |||
+ | For this, we use the '''plot''' command. | ||
− | Type '''plot brackets t | + | Type '''plot brackets t comma cos(t) '''and press '''Enter.''' |
|- | |- | ||
Line 211: | Line 215: | ||
cosine = cos(t)plot(t, cosine) | cosine = cos(t)plot(t, cosine) | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| We can also assign the value of '''cos(t) '''to a variable '''cosine''' by typing''' cosine equals to cos(t). '''Press Enter | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| We can also assign the value of '''cos(t) '''to a variable '''cosine''' by typing''' cosine equals to cos(t). '''Press '''Enter'''. |
− | And then '''plot''' | + | And then '''plot''' by typing '''plot(t comma cosine) '''and press '''Enter''' |
|- | |- | ||
Line 249: | Line 253: | ||
Show plotted '''sine curve''' | Show plotted '''sine curve''' | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| To study the '''plot''' better on the''' plot window,''' we can use various options that are available on it. Let us have a look at these options. | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| To study the '''plot''' better on the''' plot window,''' we can use various options that are available on it. |
+ | |||
+ | Let us have a look at these options. | ||
|- | |- | ||
Line 267: | Line 273: | ||
Save the '''plot''' as '''sin_curve.pdf ''' | Save the '''plot''' as '''sin_curve.pdf ''' | ||
− | | style="border:0.5pt solid #000000;padding:0.0417in;"| The right-most among them is for saving the file. Just click on it and type the '''file name'''. Let us save the '''plot''' by the name ''' | + | | style="border:0.5pt solid #000000;padding:0.0417in;"| The right-most among them is for saving the file. |
+ | |||
+ | Just click on it and type the '''file name'''. | ||
+ | |||
+ | Let us save the '''plot''' by the name '''sin underscore curve''' in '''pdf format'''. | ||
|- | |- | ||
Line 273: | Line 283: | ||
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| Click on the dropdown here. | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| Click on the dropdown here. | ||
+ | As you can see, there are many formats in which to save the '''file. | ||
− | + | Formats like '''png, eps, pdf''', and '''ps''' are available. | |
− | + | ||
We have to specify the format that we prefer at the time of saving. | We have to specify the format that we prefer at the time of saving. | ||
Line 284: | Line 294: | ||
Point the mouse on the slider '''button''' | Point the mouse on the slider '''button''' | ||
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| To the left of the '''save '''button, is the '''slider button.''' | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| To the left of the '''save '''button, is the '''slider button.''' | ||
− | |||
Using this '''button''', we can specify the margins of the''' plot window'''. | Using this '''button''', we can specify the margins of the''' plot window'''. | ||
Line 292: | Line 301: | ||
Show how to '''zoom'''. | Show how to '''zoom'''. | ||
− | |||
Press '''zoom''' '''button''' and specify region to '''zoom''' | Press '''zoom''' '''button''' and specify region to '''zoom''' | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| To the left of the '''slider button '''is the '''zoom''' '''button'''. It is used to '''zoom''' into the '''plot'''. | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| To the left of the '''slider button '''is the '''zoom''' '''button'''. |
+ | |||
+ | It is used to '''zoom''' into the '''plot'''. | ||
Just specify the region to '''zoom''' into. | Just specify the region to '''zoom''' into. | ||
Line 309: | Line 319: | ||
Press Back and Forward '''Button''' | Press Back and Forward '''Button''' | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| The next two buttons with left and right arrow icons change the state of the '''plot'''. It takes us to the previous or next state of the '''plot'''. It acts like the back and forward button in a browser. | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| The next two buttons with left and right arrow icons, change the state of the '''plot'''. |
+ | |||
+ | It takes us to the previous or next state of the '''plot'''. | ||
+ | |||
+ | It acts like the back and forward button in a browser. | ||
|- | |- | ||
Line 326: | Line 340: | ||
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Pause the video here, try out the following exercise and then resume the video. | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Pause the video here, try out the following exercise and then resume the video. | ||
− | Plot '''(sin(x) | + | Plot '''(sin(x) multiplied by sin(x)) divided by x'''. |
− | # Save the '''plot''' as '''sinsquarebyx | + | # Save the '''plot''' as '''sinsquarebyx dot pdf''' |
# '''Zoom''' and find the '''maxima'''. | # '''Zoom''' and find the '''maxima'''. | ||
# Bring it back to initial position. | # Bring it back to initial position. | ||
Line 349: | Line 363: | ||
# Clear drawing area using '''clf'''. | # Clear drawing area using '''clf'''. | ||
# Usage of buttons in the '''UI''' of the plot window such as -<br/> '''save''', '''zoom,''' '''move''' '''axis''', '''back''' and '''forward '''and '''Home ''' | # Usage of buttons in the '''UI''' of the plot window such as -<br/> '''save''', '''zoom,''' '''move''' '''axis''', '''back''' and '''forward '''and '''Home ''' | ||
− | |||
− | |||
|- | |- | ||
Line 361: | Line 373: | ||
| style="border:0.5pt solid #000000;padding:0.0417in;"| Here are some self assessment questions for you to solve - | | style="border:0.5pt solid #000000;padding:0.0417in;"| Here are some self assessment questions for you to solve - | ||
− | # Create 100 equally spaced points between ''' | + | # Create 100 equally spaced points between '''minus pi by 2''' and '''pi by 2''' |
# How can we find the length of a sequence? | # How can we find the length of a sequence? | ||
− | # What will the command '''linspace( | + | # What will the command '''linspace(minus pi comma pi comma 100)''' do? |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 381: | Line 386: | ||
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| And the answers, | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| And the answers, | ||
− | # We use the command '''linspace( | + | # We use the command '''linspace(minus pi by 2 comma pi by 2 comma 100)''' to create 100 equally spaced lines between the points '''minus pi by 2''' and '''pi by 2'''. |
− | # '''len( | + | # '''len(sequence underscore name) '''is the function used to find out the length of a sequence. |
− | # The command '''linspace( | + | # The command '''linspace(minus pi comma pi comma 100)''' will return 100 evenly spaced samples from '''-pi''' to '''pi''' including both '''minus pi '''and '''pi'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 449: | Line 452: | ||
Thank You | Thank You | ||
− | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| This is | + | | style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| This is Usha from IIT Bombay signing off. Thanks for watching. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|} | |} |
Latest revision as of 11:45, 26 February 2017
Python/C2/Creating-simple-plots-using-IPython/English
Title of script: Using plot command interactively in IPython.
Author: Thirumalesh H S
Keywords: Python, IPython, plot , pylab, matplotlib
Visual Cue | Narration |
Show slide
[Slide with MHRD logo] |
Hello friends. Welcome to the tutorial on using plot command interactively in IPython |
Show slide
Objectives
|
At the end of this tutorial, you will be able to,
|
Show slide
System Specifications |
To record this tutorial, I am using
|
Show slide
Pre-requisites |
Pre-requisites to practise this tutorial are -
You should know how to run basic Python commands on the ipython console.
|
[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.
|
Let's first understand what is Pylab | |
Show Slide
Pylab |
Pylab is a convenient Python module -
|
[iPython console]
Highlight “Using matplotlib backend: TkAgg ” |
After running percentage pylab in the iPython console you will see a message-
“Using matplotlib backend: TkAgg ”.
|
Show Slide
Error if matplotlib is not installed |
But sometimes you may get an error that says
“ImportError: No module named matplotlib”.
|
[iPython console]
linspace? |
Let's come back to the ipython console.
Press Enter
|
Type q | The displayed information says that
Press q to exit the documentation and return to the console. |
[iPython console]
linspace(1, 100, 100) >> press Enter. |
Let's try to generate 100 points from 1 to 100. Type linspace brackets 1 comma 100 comma 100
As you can see, a sequence of numbers from 1 to 100 is displayed. |
[iPython console]
linspace(0, 1, 200) >> press Enter. |
Now let's try to generate 200 points between 0 and 1.
|
In linspace, the start and stop points can be integers, decimals, or constants. | |
[iPython console]
linspace(-pi, pi, 100)
|
Let us now learn about the len function.
|
[iPython console]
t = linspace(-pi, pi, 100) |
Now, let us save this to a variable, say t.
Press Enter. |
[iPython console]
len(t) |
If we now type len bracket t and press Enter, we will get the number of points between minus pi and pi.
len function gives the no. of elements present in a given sequence. |
[iPython console]
plot(t, cos(t)) |
Next, let's try and plot a cosine curve between minus pi and pi.
For this, we use the plot command.
|
As you can see from the cosine plot, cos(t) gets the cosine value at every point corresponding to point t. | |
[iPython console]
cosine = cos(t)plot(t, cosine) |
We can also assign the value of cos(t) to a variable cosine by typing cosine equals to cos(t). Press Enter.
|
Show slide
clf function() |
To clear the plot, we have to use the clf() function.
This avoids overlapping of new plots over older plots. |
[iPython console]
clf() |
In the console, type clf() and press Enter.
|
[iPython console]
plot(t, sin(t)) |
Now, let's try to plot a sine plot.
|
[Plot window]
Show plotted sine curve |
To study the plot better on the plot window, we can use various options that are available on it.
Let us have a look at these options. |
[Plot window]
Move the mouse along the plot |
Moving the mouse pointer along the plot gives us the location of each point on the plot. |
Point to the buttons on the bottom left of the window. | Notice here.
At the bottom left of the window, there are a few buttons. |
[Plot window]
Save the plot as sin_curve.pdf |
The right-most among them is for saving the file.
Just click on it and type the file name. Let us save the plot by the name sin underscore curve in pdf format. |
Click on the drop-down to show the available formats | Click on the dropdown here.
As you can see, there are many formats in which to save the file. Formats like png, eps, pdf, and ps are available. We have to specify the format that we prefer at the time of saving. |
[Plot window]
Point the mouse on the slider button |
To the left of the save button, is the slider button.
Using this button, we can specify the margins of the plot window. |
[Plot window]
Show how to zoom. Press zoom button and specify region to zoom |
To the left of the slider button is the zoom button.
It is used to zoom into the plot. Just specify the region to zoom into. |
[Plot window]
Press Move button and move the axes. |
The button to the left of zoom can be used to move the axes of the plot. |
[Plot window]
Press Back and Forward Button |
The next two buttons with left and right arrow icons, change the state of the plot.
It takes us to the previous or next state of the plot. It acts like the back and forward button in a browser. |
[Plot window]
Press home button |
The last one is 'home' referring to the initial plot. |
Show Slide
Exercise 1
|
Pause the video here, try out the following exercise and then resume the video.
Plot (sin(x) multiplied by sin(x)) divided by x.
|
Show Slide
Summary
|
This brings us to the end of this tutorial. In this tutorial, we have learnt to,
|
Show Slide
Assignment
|
Here are some self assessment questions for you to solve -
|
Show Slide
Solutions
|
And the answers,
|
Show Slide
About the Spoken Tutorial Project
|
The video at the following link summarises the Spoken Tutorial project.
|
Show Slide
Spoken tutorial workshops |
We conduct workshops using Spoken Tutorials and give Certificates.
Please contact us. |
Show Slide
Forum to answer questions |
Do you have questions in THIS Spoken Tutorial?
Choose the minute and second where you have the question. Explain your question briefly. Someone from the FOSSEE team will answer them. Please visit this site. |
Show Slide
Forum to answer questions |
Do you have any general / technical questions?
Please visit the forum given in the link. |
Show Slide
Textbook Companion |
The FOSSEE team coordinates coding of solved examples of popular books.
We give honorarium and certificates for those who do this. For more details, please visit this site. |
Show Slide
Acknowledgement |
The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India |
Show Slide
Thank You |
This is Usha from IIT Bombay signing off. Thanks for watching. |