Python-3.4.3/C2/Embellishing-a-plot/English
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 plot inside 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 plot inside the brackets 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 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 (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
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 |