Python/C2/Embellishing-a-plot/English

From Script | Spoken-Tutorial
Revision as of 11:56, 2 December 2012 by Pravin1389 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

containing title, name of the production team along with the logo of MHRD

Welcome to the tutorial on "Embellishing a Plot".
Show Slide 2

Learning objectives

At the end of this tutorial, you will be able 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 3

Show slide with pre-requisite

open the terminal and type ipython -pylab

ipython -pylab
Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively".

Let us start ipython with pylab loaded, open the terminal and type

x = linspace(-2, 4, 20)
plot(x, sin(x))
We shall first make a simple plot and start decorating it.
clf()
plot(x, sin(x), 'r')
As we can see, the default colour and the default thickness of the line is as decided by pylab. Wouldn't it be nice if we could control these parameters in the plot? This is possible by passing additional arguments to the plot command. We shall first clear the figure and plot the same by passing the additional color argument. Pass the argument 'r' for red color.
Move the mouse pointer on the plot The same plot is seen in red color.
Switch to terminal
plot(x, cos(x), linewidth=2)
The thickness of the line can be altered by 'linewidth' argument.
Show the plot and compare the sine and cos plots A plot with line thickness 2 is produced.
Show Slide 4

Assignment 1

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

Plot sin(x) in blue color along with linewidth as 3.

Switch to terminal
clf()
plot(x, sin(x), 'b', linewidth=3)
Switch to terminal for solution A combination of color and linewidth would do the job for us.
clf()
plot(x, sin(x), '.')
To get the style of line as bunch of points not joined, pass the linestyle argument with or without color argument.
Point at the dots on the plot We get a plot with only points.
clf()
plot(x, sin(x), 'b.')
To get the same plot in blue color.
plot?

Run through the documentation and show the options available

Show the options available for line style and colors

Other available options for passing arguments can be seen in the documentation of plot.
Show Slide 5

Assignment 2

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

Plot the sine curve with green filled circles.

Switch to terminal and type the following commands
clf()
plot(x, cos(x), 'go')
Switch to terminal for solution. We use a combination of linestyle and color.
Show Slide 6

Assessment 3

Pause the video here, try out the following exercise and resume the video.

Plot the curve of x vs tan(x) in red dash line and linewidth 3.

Switch to terminal and type the following commands
clf()
plot(x, cos(x), 'r--')
Switch to terminal for solution. Here we shall use a combination of linewidth argument and linestyle.
Now that we know how to produce a bare minimum plot with color, style and thickness of our interest, we shall look at further decorating the plot.
plot(x, -x*x + 4*x - 5, 'r', linewidth=2)

Show the plot window and switch back to terminal

Let us start with a plot for the function -x^2 + 4x - 5.
Switch to terminal
title("Parabolic function -x^2+4x-5")

The title command as you can see, takes a string as an argument

Show the plot window and point to the title

As you can see, the figure does not have any description describing the plot.

To add a title to the plot to describe what the plot is,use the title command.

title("Parabolic function $-x^2+4x-5$") The figure now has a title. But it is not formatted and does not look clean.

It would look shabby if there were fractions and more complex functions like log and exp. Wouldn't it be good if the title is seen in LaTeX like formatting?

This is possible by adding a $ sign before and after the part of the string that should be in LaTeX style.

Point at the polynomial As we can see, the polynomial is now formatted.
Show Slide 7

Assignment 4

Pause the video here, try out the following exercise and resume the video.

Change the title of the figure such that the whole title is formatted in LaTeX style.

Switch to terminal
title("$Parabolic function -x^2+4x-5$")
Switch to terminal for solution. The solution is to enclose the whole string in between $.
xlabel("x")
ylabel("f(x)")
Although we have title, the plot is not complete without labelling x and y axes. we shall label x-axis to "x" and y-axis to "f(x)".
As you can see, xlabel and 'ylabel' command takes a string as an argument. xlabel sets the label to x-axis as 'x' and ylabel sets the name to the y-axis as 'f(x)'.
Show the plot window and point to xlabel and ylabel and

Unexpected indentation.

switch back to the terminal

Show Slide 8

Assignment 5

Pause the video here, try out the following exercise and resume the video.

Set the x and y labels as "x" and "f(x)" in LaTeX style.

Since we need LaTeX style formatting, all we have to do is enclose the string in between two $.

Switch to terminal
xlabel("$x$")
ylabel("$f(x)$")
Switch to terminal for solution.
Switch to terminal
annotate("local maxima", xy=(2, -1))

Show the annotation that has appeared on the plot

The plot is now almost complete. Except that the points are not named. For example the point (2, -1) is the local maxima. We would like to name the point accordingly. To do this use the function annotate.
Point at the annotate command while explaining As you can see, the first argument to annotate command is the name we would like to mark the point as, and the second argument is the co-ordinates of the point at which the name should appear. It is a tuple containing two numbers. The first is x co-ordinate and second is y co-ordinate.
Show Slide 9

Assignment 6

Pause the video, do this exercise and then resume the video.

Make an annotation called "root" at the point (-4, 0). What happens to the first annotation ?

Switch to the terminal and type the command
annotate("root", xy=(-4,0))
Switch to the terminal for the solution. As we can see, every annotate command makes a new annotation on the figure.

Now we have everything we need to decorate a plot, but the plot would be incomplete if we can not set the limits of axes. This can be done using the button provided on the plot window.

Else limits also can be get and set from the terminal. Use "xlim()" and "ylim()" functions to get the limits.

xlim()
ylim()
xlim function returns the current x axis limits and ylim function returns the current y-axis limits.

Set the limits of x-axis from -4 to 5 by giving command xlim(-4,5).

xlim(-4, 5)
ylim(-15, 2) Similarly set the limits of y-axis appropriately.
Show Slide 10

Assignment 7

Pause the video, do this exercise and then resume the video.

Set the limits of axes such that the area of interest is the rectangle (-1, -15) and (3, 0)

Switch to terminal
xlim(-1, 3)
ylim(-15, 0)
Switch to the terminal for the solution. As we can see, the lower and upper limits of x-axis in the exercise are -1 and 3 respectively. The lower and upper limits of y-axis are -15 and 0 respectively.
This gives us the required rectangle.
Show Slide 11

Summary slide

This brings us to the end of this tutorial.In this tutorial,we have learnt to,
  1. Modify the attributes of 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 part of 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 12

Self assessment questions' slide

Here are some self assessment questions for you to solve.
  1. Draw a plot of cosine graph between -2pi to 2pi with line thickness 4.
  2. Read through the documentation and find out, is there a way to modify the alignment of text in the command ylabel.
    • Yes
    • No
  1. How do you set the title as x^2-5x+6 in LaTex style formatting.


Show Slide 13

Solutions for the self assessment questions

And the answers,

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

x = linspace(-2*pi, 2*pi)
plot(x, cos(x), linewidth=4)
  1. No. We do not have an option to modify the alignment of text in the command ylabel.
  2. To set the title in LaTex style formatting,we write the equation between two dollar signs as,
title("$x^2-5x+6$")
Show Slide 14

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika, Pravin1389