Python-3.4.3/C2/Embellishing-a-plot/English

From Script | Spoken-Tutorial
Revision as of 13:25, 28 February 2017 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Python/C2/Embellishing a Plot/English


Title of script: Embellishing a Plot

Author: Pravin, Aditya Palaparthy

Keywords: Python, IPython, plot, title


Visual Cue
Narration
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-
  1. Modify the attributes of the plot -- color, line style, linewidth.
  2. Add a title to the plot with embedded LaTeX.
  3. Label x and y axes.
  4. Add annotations to the plot.
  5. Set and get the limits of axes.
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 practice this tutorial, you should know how to
  • run basic Python commands on the ipython console
  • and use the Plots interactively.

If not, see the pre-requisite Python tutorials on this website.

[Terminal]

ipython3 and press Enter.

Let us start ipython3.

Open the terminal.

Type ipython3 and press Enter.

[IPython console]

%pylab and press Enter.

Let us initialise the pylab package

Type percentage pylab and press Enter.

[IPython console]

x = linspace(-2, 4, 20) and press Enter.

We shall first make a simple plot and start modifying it.


Type x = linspace inside the brackets minus 2 comma 4 comma 20 and press Enter.

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.


The default colour and thickness of the line is as decided by pylab.

Let us now change the parameters of this curve.


We can pass additional arguments to the plot command to do this.

[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.


So we will type plot inside the brackets x comma sin(x) comma inside inverted commas r and press Enter.

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.


Do not close the plot window just minimize it.

[Ipython console] We can alter the thickness of the line by using the 'linewidth' argument.
[IPython console]


plot(x,cos(x),linewidth=2) and press Enter.

This time we will draw the cosine curve on the plot window.


So, type plot inside the brackets x comma cos(x) comma linewidth equals to 2 and press Enter.

[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


A combination of color and linewidth would do the job for us.

[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.


Type plot question mark and press Enter

Show Slide

Exercise 1


Pause the video. Try this exercise and then resume the video


Plot the curve of x verses cos(x) in red dash line and linewidth 3.

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


We use a combination of linewidth argument and linestyle.

Now we know how to produce a bare minimum plot with color, style and thickness.


Let us look at modifying the plot further.

[IPython console]

plot(x, -x*x + 4*x - 5, 'r', linewidth=2)

Let us start with a plot for the function minus x square plus 4x minus 5.


Now type plot inside the brackets x comma minus x multiplied by x plus 4 multiplied by x minus 5 comma inside inverted commas r comma linewidth equals to 2

[Plot window] We will see the curve of this equation in the plot window.


But the figure does not have any description describing the plot.

[IPython console]

title("Parabolic function -x^2+4x-5")

To add a title to the plot, use the title command.


So type title inside the brackets inside inverted commas Parabolic function minus x square plus 4x minus 5

[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.


But it is not formatted and does not look clean.

Show the plot window and point to the title It would look even more shabby if there were fractions and complex functions.


Let us write the title in LaTeX format for a neater look.

[IPython console]

title(r"Parabolic function $-x^2+4x-5$")

For LaTeX format, we put a dollar sign before and after the string.


Type title inside the brackets r inside inverted commas Parabolic function dollar minus x square plus 4x minus 5 dollar


Here ' r ' means that the string is to be treated as a raw 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.


Type xlabel inside the brackets r inside inverted commas dollar x dollar and ylabel inside brackets r inside inverted commas dollar y dollar

[IPython console]


annotate("local maxima", xy=(2, -1))


The plot is now almost complete.


Now we will name the points considering point(2 comma minus 1) as local maxima.


To name a point, we use the function annotate.


Type annotate inside the brackets inside inverted commas local maxima comma xy equals to inside brackets 2 comma minus 1

[Plot Window]


Show the annotation that has appeared on the plot

We can see the local maxima at point (2 comma minus 1)
[IPython console]


Point at the annotate command while explaining

The first argument in annotate command is the name of the point.


The second argument represents the coordinates of the point.


It is a tuple containing two numbers. The first is x coordinate and second is y coordinate.

[IPython console]

xlim()

ylim()

Next, type xlim brackets

xlim function returns the current x axis limits.


Then type ylim brackets

ylim function returns the current y-axis limits.

[IPython console]


xlim(-4, 5)

ylim(-15, 2)

Set the limits of x-axis from minus 4 to 5 by typing xlim inside the brackets minus 4 comma 5


Similarly, set the limits of y-axis in a similar manner.


Type ylim inside the brackets minus 15 comma 2.

Show Slide

Exercise 2


Pause the video. Try this exercise and then resume the video.


Make an annotation called "root" at the point (minus 4 comma 0).

What happens to the first annotation?

[IPython console]


annotate("root", xy=(-4,0))


Switch to the Ipython console for the solution.


Type annotate inside the brackets inside inverted commas root comma xy equals to inside brackets minus 4 comma 0


Every annotate command makes a new annotation on the figure.

Show Slide

Summary slide


This brings us to the end of this tutorial. In this tutorial, we have learnt to,


  1. Modify the attributes of a plot like color, line width, line style by passing additional arguments.
  2. Add title to a plot using 'title command.
  3. Incorporate LaTeX style formatting by adding a $ sign before and after the string.
  4. Label x and y axes using xlabel() and ylabel() commands.
  5. Add annotations to a plot using annotate() command.
  6. Get and set the limits of axes using xlim() and ylim() commands.


Show Slide

Evaluation

Here are some self assessment questions for you to solve.
  1. Draw a plot of cosine graph between minus 2pi to 2pi with line thickness 4.
  2. Read the documentation and find out, is there a way to modify the alignment of text in the command ylabel.
Show Slide

Solutions


And the answers,

1. In order to plot a cosine graph between the points minus 2pi and 2pi with line thickness 4,we use the linspace and plot command as,

x equals to linspace inside the brackets minus 2pi comma 2pi.

plot (x comma cos(x) comma linewidth equals to 4)

The answer to the second question is:

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?


Please visit the FOSSEE forum and post your question.

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

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Vineeta