Difference between revisions of "Python-3.4.3/C2/Embellishing-a-plot/English"
(Created page with "'''Python/C2/Embellishing a Plot/English''' '''Title of script''': '''Embellishing a Plot''' '''Author: '''[http://script.spoken-tutorial.org/index.php?title=User:Chandrika...") |
|||
Line 78: | Line 78: | ||
− | Type '''%pylab '''and press''' Enter''' | + | Type '''%pylab '''and press''' Enter.''' |
|- | |- | ||
Line 87: | Line 87: | ||
− | Type '''x = linspace inside the brackets | + | Type '''x = linspace inside the brackets minus 2 comma 4 comma 20 '''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;"| '''plot(x, sin(x)) '''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;"| '''plot(x, sin(x)) '''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;"| Then type '''ploti nside the brackets | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Then type '''ploti nside the brackets x comma sin(x) '''and press '''Enter.''' |
|- | |- | ||
Line 124: | Line 124: | ||
− | So we will type '''plot inside the brackets | + | So we will type '''plot inside the brackets x comma sin(x) comma inside inverted comma r '''and press '''Enter.''' |
Here, the '''argument 'r' '''is for red color. | Here, the '''argument 'r' '''is for red color. | ||
Line 150: | Line 150: | ||
− | So, type '''plot inside the brackets | + | So, type '''plot inside the brackets x comma cos(x) comma linewidth equals to 2 '''and press '''Enter.''' |
|- | |- | ||
Line 178: | Line 178: | ||
'''plot(x, sin(x), 'b', linewidth=3)''' | '''plot(x, sin(x), 'b', linewidth=3)''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now type''' inside the brackets plot | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now type''' inside the brackets plot x comma sin(x) comma inside inverted commas b comma linewidth equals to 3''' |
Line 200: | Line 200: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now type '''plot inside the brackets | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now type '''plot inside the brackets plot x comma sin(x) comma inside inverted commas dot.''' |
|- | |- | ||
Line 248: | Line 248: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now type '''plot | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now type '''plot inisde brackets x comma cos(x) comma inside inverted commas r hypen hypen comma linewidth equals to 3''' |
Line 267: | Line 267: | ||
− | Now type '''plot | + | Now type '''plot inside brackets x comma minus x multiplied by x plus 4 multiplied by x minus 5 comma inisde inverted commas r comma linewidth equals to 2''' |
|- | |- | ||
Line 283: | Line 283: | ||
− | So type '''title | + | So type '''title inside the brackets inside inverted commas Parabolic function minus x square plus 4x minus 5''' |
|- | |- | ||
Line 312: | Line 312: | ||
− | Type '''title | + | Type '''title inside the brackets r inside inverted commas Parabolic function dollar minus x square plus 4x minus 5''' |
Line 330: | Line 330: | ||
'''xlabel(r"$x$") ''' | '''xlabel(r"$x$") ''' | ||
− | '''ylabel(r"$ | + | '''ylabel(r"$y$")''' |
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Although we have '''title''', the '''plot''' is not complete without labelling the '''x '''and '''y axes'''. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Although we have '''title''', the '''plot''' is not complete without labelling the '''x '''and '''y axes'''. | ||
Line 336: | Line 336: | ||
− | Type '''xlabel | + | Type '''xlabel inside the brackets r insde inverted commas dollar x dollar '''and''' ylabel inside the brackets r insde inverted commas dollar y dollar ''' |
|- | |- | ||
Line 355: | Line 355: | ||
− | Type '''annotate | + | Type '''annotate inside the brackets insde inverted commas local maxima comma xy equals to inside brackets 2 comma -1''' |
|- | |- | ||
Line 378: | Line 378: | ||
|- | |- | ||
− | | 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;"| [IPython console] |
− | + | '''xlim() ''' | |
+ | '''ylim()''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Next, type '''xlim brackets''' | ||
+ | '''xlim function '''returns the current '''x axis limits.''' | ||
− | |||
+ | Then type '''ylim brackets''' | ||
− | + | '''ylim function''' returns the current '''y-axis limits'''. | |
− | + | ||
− | + | ||
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [IPython console] | + | | style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [IPython console] |
− | ''' | + | '''xlim(-4, 5) ''' |
+ | '''ylim(-15, 2)''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Set the '''limits of x-axis''' from -4 to 5 by typing '''xlim inisde the brackets minus 4 comma 5''' | ||
− | + | Similarly, set the '''limits of y-axis''' in a similar manner. | |
− | Type ''' | + | Type '''ylim inside the brackets minus 15 comma 2.''' |
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Show Slide | ||
− | + | Exercise 2 | |
− | |||
− | |||
− | |||
− | + | | style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Pause the video. Try this exercise and then resume the video. | |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| | + | |
− | |||
+ | Make an '''annotation''' called "'''root'''" at the '''point (-4, 0)'''. | ||
− | + | What happens to the first '''annotation'''? | |
− | + | ||
− | + | ||
|- | |- | ||
Line 426: | Line 425: | ||
− | ''' | + | '''annotate("root", xy=(-4,0))''' |
− | |||
− | |||
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Switch to the '''Ipython console '''for the solution. | |
− | Type ''' | + | Type '''annotate inside brackets inisde inverted commas root comma xy equals to inside brackets minus -4 comma 0''' |
+ | |||
+ | |||
+ | Every '''annotate''' '''command''' makes a new '''annotation''' on the figure. | ||
|- | |- | ||
Line 483: | Line 483: | ||
| 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, | ||
− | 1. In order to '''plot''' a '''cosine graph''' between the points -2pi and 2pi with line thickness | + | 1. In order to '''plot''' a '''cosine graph''' between the points -2pi and 2pi with line thickness 4,we use the '''linspace''' and''' plot''' '''command '''as, |
− | '''x = linspace | + | '''x = linspace inside the brackets minus 2*pi comma 2*pi.''' |
− | '''plot | + | '''plot inside the brackets x comma cos(x) comma linewidth equals to 4)''' |
2. No. We do not have an option to modify the '''alignment '''of text in the '''command''' '''ylabel'''. | 2. No. We do not have an option to modify the '''alignment '''of text in the '''command''' '''ylabel'''. |
Revision as of 13:06, 15 February 2017
Python/C2/Embellishing a Plot/English
Title of script: Embellishing a Plot
Author: Chandrika, Pravin, Aditya Palaparthy
Keywords: Python, IPython, plot, title
|
|
Title Slide | Hello Friends. Welcome to the tutorial on "Embellishing a Plot". |
Show Slide
Objectives
|
At the end of this tutorial, we will learn to-
|
Show Slide
System Requirements |
To record this tutorial, I am using
|
Show slide
Pre-requisites |
To practice this tutorial, you should know how to
If not, see the pre-requisite Python tutorials on this website. |
[Terminal]
ipython3 and press Enter. |
Let us start ipython3.
|
[IPython console]
%pylab and press Enter. |
Let us initialise the pylab package
|
[IPython console]
x = linspace(-2, 4, 20) and press Enter. |
We shall first make a simple plot and start modifying it.
|
plot(x, sin(x)) and press Enter. | Then type ploti nside the brackets x comma sin(x) and press Enter. |
[Plot Window] | We can now see the sine curve in the plot window.
|
Let us now change the parameters of this curve.
| |
[IPython console]
clf() and press Enter. |
We shall first clear the plot window by typing clf() in the ipython console. |
[Plot window] | You can now see a blank plot window. |
[IPython console]
plot(x, sin(x), 'r') and press Enter. |
Now plot the same sine curve with an additional color argument.
Here, the argument 'r' is for red color. |
Show the sine curve on the plot window by moving the mouse pointer on it.
|
On the plot window, the same sine curve is now seen in red color.
|
[Ipython console] | We can alter the thickness of the line by using the 'linewidth' argument. |
[IPython console]
|
This time we will draw the cosine curve on the plot window.
|
[Plot window]
compare the sine and cosine plots |
A cosine curve with line thickness 2 is produced in the plot window. |
Let us now try to plot a sine curve in blue color with linewidth as 3. | |
[IPython console] | Here onwards, press the Enter the key to execute every command that we type on the Ipython console. |
[IPython console]
clf() |
First let us clear the plot window by typing clf().
You will again see a blank plot window |
[IPython console]
plot(x, sin(x), 'b', linewidth=3) |
Now type inside the brackets plot x comma sin(x) comma inside inverted commas b comma linewidth equals to 3
|
[Ipython console] | To get the plot in dotted style instead of a solid style, put a dot in linestyle. |
[IPython console]
clf() |
First, type clf() to clear the plot window. |
[IPython console]
plot(x, sin(x), '.')
|
Now type plot inside the brackets plot x comma sin(x) comma inside inverted commas dot. |
[Plot Window] | We get the sine curve in dotted style. |
[IPython console]
plot? Run through the information and show the options available Show the options available for line style and colors |
Let's see the information of plot.
|
Show Slide
Exercise 1
|
Pause the video. Try this exercise and then resume the video
|
Let us switch to the console for the solution. | |
[IPython console]
clf() |
Type clf() to clear the plot window. |
[IPython console]
plot(x, cos(x), 'r--',linewidth=3)
|
Now type plot inisde brackets x comma cos(x) comma inside inverted commas r hypen hypen comma linewidth equals to 3
|
Now we know how to produce a bare minimum plot with color, style and thickness.
| |
[IPython console]
plot(x, -x*x + 4*x - 5, 'r', linewidth=2) |
Let us start with a plot for the function -x^2 + 4x - 5.
|
[Plot window] | We will see the curve of this equation in the plot window.
|
[IPython console]
title("Parabolic function -x^2+4x-5") |
To add a title to the plot, use the title command.
|
[IPython console]
Highlight the title command |
The title command as you can see, takes a string as an argument. |
Show the plot window and point to the title | We can see the title on the plot window.
|
Show the plot window and point to the title | It would look even more shabby if there were fractions and complex functions.
|
[IPython console]
title(r"Parabolic function $-x^2+4x-5$") |
For LaTeX format, we put a $ before and after the string.
It will ignore all escape codes. |
[Plot Window]
Point at the polynomial |
As we can see that the polynomial in the title is now formatted. |
[IPython console]
xlabel(r"$x$") ylabel(r"$y$") |
Although we have title, the plot is not complete without labelling the x and y axes.
So we will label x and y axes in Latex style.
|
[IPython console]
|
The plot is now almost complete.
|
[Plot Window]
|
We can see the local maxima at point (2, -1) |
[IPython console]
|
The first argument in annotate command is the name of the point.
|
[IPython console]
xlim() ylim() |
Next, type xlim brackets
xlim function returns the current x axis limits.
ylim function returns the current y-axis limits. |
[IPython console]
ylim(-15, 2) |
Set the limits of x-axis from -4 to 5 by typing xlim inisde the brackets minus 4 comma 5
|
Show Slide
Exercise 2
|
Pause the video. Try this exercise and then resume the video.
What happens to the first annotation? |
[IPython console]
|
Switch to the Ipython console for the solution.
|
Show Slide
Summary slide
|
This brings us to the end of this tutorial. In this tutorial, we have learnt to,
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve.
|
Show Slide
Solutions
|
And the answers,
1. In order to plot a cosine graph between the points -2pi and 2pi with line thickness 4,we use the linspace and plot command as, x = linspace inside the brackets minus 2*pi comma 2*pi. plot inside the brackets x comma cos(x) comma linewidth equals to 4) 2. No. We do not have an option to modify the alignment of text in the command ylabel. |
Show Slide
About the Spoken Tutorial Project
|
The video in the following link summarises the Spoken Tutorial project.
|
Show Slide
Spoken tutorial workshops |
We conduct workshops using Spoken Tutorials and give Certificates.
|
Show Slide
Forum to answer questions |
Do you have questions on 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 on Python?
|
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 (________) from IIT Bombay signing off. Thanks for watching |