Python-3.4.3/C2/Embellishing-a-plot/English-timed
From Script | Spoken-Tutorial
Revision as of 13:36, 12 June 2019 by Sandhya.np14 (Talk | contribs)
00:01 | Hello friends! Welcome to the tutorial on "Embellishing a Plot". |
00:06 | At the end of this tutorial, we will learn to- modify the attributes of the plot -color, line style, linewidth, |
00:16 | add a title to the plot with embedded LaTeX, |
00:20 | label x and y axes, add annotations to the plot, |
00:26 | set and get the limits of axes. |
00:30 | To record this tutorial, I am using:
Ubuntu Linux 14.04 operating system, |
00:37 | Python 3.4.3,
IPython 5.1.0 |
00:43 | To practice this tutorial, you should know how to run basic Python commands on the ipython console and use the Plots interactively. |
00:54 | If not, see the pre-requisite Python tutorials on this website. |
00:59 | Let us start ipython3.
Open the terminal. |
01:05 | Type ipython3 and press Enter. |
01:11 | Let us initialise the pylab package. |
01:15 | Type: percentage pylab and press Enter. |
01:21 | We shall first make a simple plot and start modifying it. |
01:26 | Type: x = linspace inside the brackets minus 2 comma 4 comma 20 and press Enter. |
01:40 | Then type plot inside the brackets x comma sin(x) and press Enter. |
01:49 | We can now see the sine curve in the plot window. |
01:53 | The default colour and thickness of the line is as decided by pylab. |
01:59 | Let us now change the parameters of this curve. |
02:03 | We can pass additional arguments to the plot command to do this. |
02:09 | We shall first clear the plot window by typing clf() in the ipython console. |
02:16 | You can now see a blank plot window. |
02:20 | Now, plot the same sine curve with an additional color argument. |
02:26 | 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. |
02:44 | On the plot window, the same sine curve is now seen in red color. |
02:50 | Do not close the plot window, just minimize it. |
02:54 | We can alter the thickness of the line by using the 'linewidth' argument. |
03:00 | This time we will draw the cosine curve on the plot window. |
03:05 | So, type plot inside brackets x comma cos(x) comma linewidth is equal to 2 and press Enter. |
03:18 | A cosine curve with line thickness 2 is produced in the plot window. |
03:24 | Let us now try to plot a sine curve in blue color with linewidth as 3. |
03:31 | Here onwards, press the Enter key to execute every command that we type on the Ipython console. |
03:39 | Let us first clear the plot window by typing clf(). |
03:44 | You will again see a blank plot window. |
03:48 | Now type plot inside the brackets x comma sin(x) comma inside inverted commas b comma linewidth is equal to 3. |
04:03 | A combination of color and linewidth would do the job for us. |
04:08 | To get the plot in dotted style instead of a solid style, put a dot in linestyle. |
04:16 | First, type clf() to clear the plot window. |
04:20 | Now type plot inside the brackets x comma sin(x) comma inside inverted commas dot. |
04:32 | We get the sine curve in dotted style. |
04:36 | Let's see the information of plot. |
04:40 | Type: plot question mark and press Enter. |
04:47 | Pause the video. Try this exercise and then resume the video . |
04:52 | Plot the curve of x verses cos(x) in red dash line and linewidth 3. |
05:00 | Let us switch to the console for the solution. |
05:04 | Type clf() to clear the plot window. |
05:08 | Now type plot inside the brackets x comma cos(x) comma inside inverted commas r hyphen hyphen comma linewidth equals to 3. |
05:25 | We use a combination of linewidth argument and linestyle. |
05:30 | Now we know how to produce a bare minimum plot with color, style and thickness. |
05:38 | Let us look at modifying the plot further. |
05:42 | Let us start with a plot for the function minus x square plus 4x minus 5. |
05:51 | 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 is equal to 2. |
06:16 | We will see the curve of this equation in the plot window. |
06:21 | But the figure does not have any description describing the plot. |
06:26 | To add a title to the plot, use the title command. |
06:31 | So, type title inside the brackets inside inverted commas Parabolic function minus x square plus 4x minus 5. |
06:48 | The title command, as you can see, takes a string as an argument. |
06:54 | We can see the title on the plot window. But it is not formatted and does not look clean. |
07:03 | It would look even more shabby if there were fractions and complex functions. |
07:09 | Let us write the title in LaTeX format for a neater look. |
07:14 | For LaTeX format, we put a dollar sign before and after the string. |
07:20 | Type title inside the brackets r inside inverted commas Parabolic function dollar minus x square plus 4x minus 5 dollar. |
07:38 | Here, 'r' means that the string is to be treated as a raw string. |
07:45 | It will ignore all escape codes. |
07:49 | As we can see that the polynomial in the title is now formatted. |
07:55 | Although we have title, the plot is not complete without labelling the x and y axes. |
08:03 | So, we will label x and y axes in LaTeX style. |
08:09 | Type xlabel inside the brackets r inside inverted commas dollar x dollar and ylabel inside brackets r inside inverted commas dollar y dollar . |
08:30 | The plot is now almost complete. |
08:34 | Now we will name the points considering point(2 comma minus 1) as local maxima. |
08:42 | To name a point, we use the function annotate. |
08:46 | Type annotate inside the brackets inside inverted commas local maxima comma xy equals to inside brackets 2 comma minus 1. |
09:03 | We can see the local maxima at point 2 comma minus 1. |
09:09 | The first argument in annotate command is the name of the point. |
09:15 | The second argument represents the coordinates of the point. |
09:20 | It is a tuple containing two numbers. The first is x coordinate and second is y coordinate. |
09:29 | Next, type xlim brackets
xlim function returns the current x axis limits. |
09:39 | Then type ylim brackets. ylim function returns the current y-axis limits. |
09:49 | Set the limits of x-axis from minus 4 to 5 by typing xlim inside the brackets minus 4 comma 5. |
10:02 | Similarly, set the limits of y-axis in a similar manner. |
10:07 | Type ylim inside the brackets minus 15 comma 2. |
10:19 | Pause the video. Try this exercise and then resume the video. |
10:24 | Make an annotation called "root" at the point (minus 4 comma 0). |
10:31 | What happens to the first annotation? |
10:35 | Switch to the Ipython console for the solution. |
10:39 | Type annotate inside the brackets inside inverted commas root comma xy is equal to minus 4 comma 0. |
10:53 | Every annotate command makes a new annotation on the figure. |
10:59 | This brings us to the end of this tutorial. In this tutorial, we have learnt to:
modify the attributes of a plot like color, line width, line style by passing additional arguments, |
11:16 | add title to a plot using 'title command, |
11:20 | incorporate LaTeX style formatting by adding a $ sign before and after the string, |
11:28 | label x and y axes using xlabel() and ylabel() commands, |
11:34 | add annotations to a plot using annotate() command, |
11:39 | get and set the limits of axes using xlim() and ylim() commands. |
11:46 | Here are some self assessment questions for you to solve. |
11:51 | Draw a plot of cosine graph between minus 2pi to 2pi with line thickness 4. |
12:00 | Read the documentation and find out, is there a way to modify the alignment of text in the command ylabel. |
12:09 | And the answers are-
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. |
12:31 | plot (x comma cos(x) comma linewidth equals to 4) |
12:38 | The answer to the second question is:
No. We do not have an option to modify the alignment of text in the command ylabel. |
12:48 | Do you have questions on THIS Spoken Tutorial? |
12:51 | Choose the minute and second where you have the question. |
12:55 | Explain your question briefly.
Someone from the FOSSEE team will answer them. Please visit this site. |
13:03 | Do you have any general / technical questions on Python? |
13:08 | Please visit the FOSSEE forum and post your question. |
13:12 | The FOSSEE team coordinates coding of solved examples of popular books. |
13:18 | We give honorarium and certificates for those who do this.
For more details, please visit this website. |
13:27 | The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India. |
13:34 | This is Usha from IIT Bombay, signing off. Thanks for watching. |