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

From Script | Spoken-Tutorial
Revision as of 12:35, 7 March 2013 by Sneha (Talk | contribs)

Jump to: navigation, search
Timing Narration
0:00 Hello friends Welcome to the tutorial on "Embellishing a Plot".
0:06 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.


0:27 So, Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively".
0:34 so,let us start ipython with pylab loaded, open the terminal and type ipython hyphen pylab.
0:48 We shall first make a simple plot and start decorating it.
0:54 So type x is equal to linspace within brackets -2,4,20
1:06 then type plot(x,sin(x))
1:15 As we can see, the default colour and the default thickness of the line is as decided by pylab.
1:23 Wouldn't it be nice if we could control these parameters in the plot?
1:28 This is possible by passing additional arguments to the plot command.
1:33 We shall first clear the figure and plot the same by passing the additional color argument.
1:39 Pass the argument 'r' for red color.
1:44 So type clf, then plot within brackets x,sin(x),within single quotes r.
2:13 The same plot is seen in red color.
2:16 The thickness of the line can be altered by 'linewidth' argument.
2:20 So type plot within brackets x,cos(x),linewidth is equal to 2
2:34 Now, a plot with line thickness 2 is produced.
2:40 Pause the video here and do this exercise and then resume the video.
2:45 Plot sin(x) in blue color along with linewidth as 3.
2:53 So, now switch to terminal for solution A combination of color and linewidth would do the job for us.
3:01 So , type clf , then type plot x, sin(x),within single quotes b,linewidth is equal to 3.
3:16 To get the style of line as bunch of points not joined, pass the linestyle argument with or without color argument.
3:25 So for that type on the terminal clf, then type plot x,sin(x), dot in single quotes.
3:43 We get a plot with only points.
3:49 To get the same plot in blue color type clf, then type plot x, sin(x),within single quotes b dot.
4:02 Other available options for passing arguments can be seen in the documentation of plot.
4:07 To see that we can type in the terminal plot then question mark.
4:19 So, you can actually go through the documentation.


4:23 So ,pause the video here and do this exercise and then resume the video.
4:28 Plot the sine curve with green filled circles.
4:33 so ,for solutionNow, switch the terminal . We use a combination of linestyle and color.
4:40 So ,type clf() then type plot within brackets x,cos(x), within single quotes go.
4:56 So, Pause the video here, try out the following exercise and resume the video.
5:02 Plot the curve of x versus tan(x) in red dash line and linewidth 3.
5:13 So for solution, we will switch to terminal .
5:18 Here we shall use a combination of linewidth argument and linestyle.
5:22 So in terminal you can type clf() then plot within brackets x, cos(x), within single quotes r hyphen hyphen
5:36 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.
5:46 Let us start with a plot for the function minus x squared plus 4x minus 5.
5:52 So for that you have to type clf then plot within brackets x,minus x star x plus 4 star x minus 5,'r',linewidth is equal to 2.
6:16 As you can see, the figure does not have any description describing the plot.
6:21 To add a title to the plot to describe what the plot is,use the title command.
6:26 So, we can type in the terminal title within brackets and double quotes Parabolic function - x squared plus 4x minus 5
6:42 The figure now has a title.
6:45 But it is not formatted and does not look clean.
6:49 It would look shabby if there were fractions and more complex functions like log and exp.
6:57 So, Wouldn't it be good if the title is seen in LaTeX like formatting?
7:03 This is possible by adding a '$' sign before and after the part of the string that should be in LaTeX style.
7:10 So in the command you can type title within brackets Parabolic function dollar sign minus x squared plus 4x minus 5 dollar sign


7:26 As we can see, the polynomial is now formatted.
7:30 So, Pause the video here, try out the following exercise and resume the video.
7:35 Change the title of the figure such that the whole title is formatted in LaTeX style.
7:41 So for that Switch to terminal for solution.
7:45 The solution is to enclose the whole string in between '$'.
7:51 So you can type title within brackets dollar sign Parabolic function -x squared plus 4x minus 5 dollar sign.
8:01 Although we have title, the plot is not complete without labelling x and y axes.
8:05 we shall label x-axis to "x" and y-axis to "f(x)".
8:12 So for that you can type in terminal xlabel within brackets in double quotes x , then ylabel in terminal within brackets in double quotes f of x.
8:31 As you can see, xlabel and 'ylabel' command takes a string as an argument.
8:37 xlabel sets the label to x-axis as 'x' and ylabel sets the name to the y-axis as 'f(x)'.


8:50 So now Pause the video here, try out the following exercise and resume the video.
8:57 Set the x and y labels as "x" and "f(x)" in LaTeX style.
9:04 Since we need LaTeX style formatting, all we have to do is enclose the string in between two '$'.
9:10 Switch to terminal and type xlabel within brackets in double quote in between two dollar sign x and then type ylabel and again brackets double quotes in between two dollar sign f of x .


9:31 The plot is now almost complete except that the points are not named.
9:37 For example the point (2, -1) is the local maxima.
9:42 We would like to name the point accordingly.
9:47 To do this use the function annotate.
9:49 So for that you can type in the terminal annotate within brackets in double quotes local maxima comma xy is equal to within brackets 2 comma -1.
10:04 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.
10:18 It is a tuple containing two numbers.
10:20 The first is x co-ordinate and second is y co-ordinate.
10:25 Pause the video, do this exercise and then resume the video.
10:30 Make an annotation called "root" at the point (-4, 0).
10:38 What happens to the first annotation ?
10:43 For that Switch to the terminal for the solution.
10:46 As we can see, every annotate command makes a new annotation on the figure.
10:52 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.
11:01 This can be done using the button provided on the plot window.
11:06 Else limits also can be get and set from the terminal.
11:13 Use "xlim()"function and "ylim()" function to get the limits.
11:17 So type in the terminal annotate within brackets in double quotes root comma xy is equal to within brackets minus 4 comma 0.
11:32 xlim function returns the current x axis limits and ylimfunction returns the current y-axis limits.
11:41 Set the limits of x-axis from -4 to 5 by giving command xlim(-4,5). So in the terminal you can type xlim() and then again ylim () then type xlim(-4,5).
12:12 Similarly set the limits of y-axis appropriately.So you can type ylim(-15,2)
12:22 Pause the video, do this exercise and then resume the video.
12:27 Set the limits of axes such that the area of interest is the rectangle (-1, -15) and (3, 0)
12:37 Switch to the terminal for the solution.


12:40 As we can see, the lower and upper limits of x-axis in the exercise are -1 and 3 respectively.
12:46 The lower and upper limits of y-axis are -15 and 0 respectively.
12:51 So in the command we can type xlim within brackets -1 comma 3 and ylim within brackets -15 comma 0.
13:02 This gives us the required rectangle.
13:09 This brings us to the end of this tutorial.In this tutorial,we have learnt to,Modify the attributes of plot like color, line width, line style by passing additional arguments.
13:20 Add title to a plot using 'title' command.
13:24 Incorporate LaTeX style formatting by adding a $ sign before and after the part of the string.
13:30 Label x and y axes using xlabel()function and ylabel() commands.
13:36 Then, add annotations to a plot using annotate() command.
13:38 Get and set the limits of axes using xlim() and ylim() commands.
13:46 Here are some self assessment questions for you to solve.
13:50 1. Draw a plot of cosine graph between -2pi to 2pi with line thickness 4.
13:57 2. Read through the documentation and find out, is there a way to modify the alignment of text in the command ylabel.
14:05 Yes or No are the options.
14:07 And the final question. How do you set the title as x^2-5x+6 in LaTex style formatting.


14:15 Now, the answers,
14:20 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)
14:41 then plot(x, cos(x), linewidth=4)
14:46 And the second answer is No. We do not have an option to modify the alignment of text in the command ylabel.
14:53 Then the third and final one. To set the title in LaTex style formatting,we write the equation between two dollar signs as,
title("$x^2-5x+6$")
15:11 Hope you have enjoyed this tutorial and found it useful.


Contributors and Content Editors

Gaurav, Kavita salve, Minal, PoojaMoolya, Sandhya.np14, Sneha