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

From Script | Spoken-Tutorial
Revision as of 18:00, 20 February 2017 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
00:00 Hello friends. Welcome to the tutorial on Embellishing a Plot.
00:06 At the end of this tutorial, you will be able to-

Modify the attributes of the plot -- color, line style, line width. Add a title to the plot with embedded LaTeX. Label X and Y axes. Add annotations to the plot. Set and Get the limits of axes.

00:27 So, before beginning this tutorial, we would suggest you to complete the tutorial on Using plot interactively.
00:34 So, let us start ipython with pylab loaded. Open the terminal and type: ipython hyphen pylab.
00:48 We shall first make a simple plot and start decorating it.
00:54 So, type: x is equal to linspace within brackets -2, 4, 20.
01:06 Then type plot(x, sin(x)).
01:15 As we can see, the default colour and the default thickness of the line is as decided by pylab.
01:23 Wouldn't it be nice if we could control these parameters in the plot?
01:28 This is possible by passing additional arguments to the plot command.
01:33 We shall first clear the figure and plot the same by passing the additional color argument.
01:39 Pass the argument 'r' for red color.
01:44 So, type clf, then plot within brackets x, sin(x), within single quotes r.
02:13 The same plot is seen in red color.
02:16 The thickness of the line can be altered by 'linewidth' argument.
02:20 So, type: plot within brackets x, cos(x), linewidth is equal to 2.
02:34 Now, a plot with line thickness 2 is produced.
02:40 Pause the video here and do this exercise and then resume the video.
02:45 Plot sin(x) in blue color along with linewidth as 3.
02:53 So, now switch to terminal for solution. A combination of color and line width would do the job for us.
03:01 So, type clf , then type plot x, sin(x), within single quotes b, linewidth is equal to 3.
03:16 To get the style of line as bunch of points not joined, pass the linestyle argument with or without color argument.
03:25 So, for that, type on the terminal clf, then type: plot x, sin(x), dot in single quotes.
03:43 We get a plot with only points.
03:49 To get the same plot in blue color, type: clf, then type plot x, sin(x),within single quotes b dot.
04:02 Other available options for passing arguments can be seen in the documentation of plot.
04:07 To see that, we can type in the terminal- plot then question mark.
04:19 So, you can actually go through the documentation.
04:23 So, pause the video here and do this exercise and then resume the video.
04:28 Plot the sine curve with green filled circles.
04:33 So, for solution, now switch to the terminal. We use the combination of linestyle and color.
04:40 So, type clf() then type plot within brackets x, cos(x), within single quotes go.
04:56 So, pause the video here. Try out the following exercise and resume the video.
05:02 Plot the curve of 'x' versus 'tan(x)', in red dash line and linewidth 3.
05:13 So, for solution, we will switch to terminal.
05:18 Here, we shall use a combination of linewidth argument and linestyle.
05:22 So, in terminal, you can type clf() then plot within brackets x, cos(x), within single quotes r hyphen hyphen.
05: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.
05:46 Let us start with a plot for the function minus x squared plus 4x minus 5.
05:52 So, for that, you have to type: first clf then plot within brackets x, minus x star x plus 4 star x minus 5, 'r', linewidth is equal to 2.
06:16 As you can see, the figure does not have any description describing the plot.
06:21 To add a title to the plot to describe what the plot is, use the title command.
06:26 So, we can type in the terminal, title within brackets and double quotes Parabolic function - x squared plus 4x minus 5
06:42 The figure now has a title.
06:45 But, it is not formatted and does not look clean.
06:49 It would look shabby if there were fractions and more complex functions like log and exp.
06:57 So, Wouldn't it be good if the title is seen in LaTeX like formatting?
07:03 This is possible by adding a '$' sign before and after the part of the string that should be in LaTeX style.
07:10 So, in the command you can type: title within brackets Parabolic function dollar sign minus x squared plus 4x minus 5 dollar sign.
07:26 As we can see, the polynomial is now formatted.
07:30 So, pause the video here. Try out the following exercise and resume the video.
07:35 Change the title of the figure such that the whole title is formatted in LaTeX style.
07:41 So, for that, switch to terminal for solution.
07:45 The solution is to enclose the whole string in between '$'.
07:51 So, you can type: title within brackets dollar sign Parabolic function -x squared plus 4x minus 5 dollar sign.
08:01 Although we have title, the plot is not complete without labeling x and y axes.
08:05 we shall label x-axis to "x" and y-axis to "f(x)".
08:12 So, for that, you can type in terminal: xlabel within brackets in double quotes x , and then ylabel in terminal within brackets in double quotes f of x.
08:31 As you can see, xlabel and ylabel commandd take a string as an argument.
08:37 xlabel sets the label to x-axis as 'x' and ylabel sets the name to the y-axis as 'f(x)'.
08:50 So, now pause the video here, try out the following exercise and resume the video.
08:57 Set the x and y labels as "x" and "f(x)" in LaTeX style.
09:04 Since we need LaTeX style formatting, all we have to do is enclose the string in between two dollar symbols ($).
09:10 So now, switch to terminal and type: xlabel within brackets in double quotes in between two dollar signs x and then type ylabel and again brackets double quotes in between two dollar signs f of x.
09:31 The plot is now almost complete except that the points are not named.
09:37 For example, the point (2, -1) is the local maxima.
09:42 We would like to name the point accordingly.
09:47 To do this, use the function annotate().
09: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 ylim() function 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 4, 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