Difference between revisions of "Python/C2/Multiple-plots/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 9: | Line 9: | ||
|- | |- | ||
| 00:05 | | 00:05 | ||
− | | At the end of this tutorial, you will be able to, | + | | At the end of this tutorial, you will be able to, draw multiple plots which are overlaid. |
− | + | use the figure command. use the legend command | |
− | + | ||
− | + | switch between the plots and perform some operations on each of them like saving the plots. Create and switch between subplots | |
− | + | ||
− | + | ||
|- | |- | ||
Line 67: | Line 65: | ||
|- | |- | ||
|01:43 | |01:43 | ||
− | | Is it not? | + | | Is it not? What really caused this? |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 270: | Line 264: | ||
|- | |- | ||
− | | | + | |09:43 |
| Save each of them. | | Save each of them. | ||
Line 302: | Line 296: | ||
|- | |- | ||
− | | | + | | 10:22 |
| Now use the figure command to create second plotting area and plot the figure | | Now use the figure command to create second plotting area and plot the figure | ||
Line 371: | Line 365: | ||
|- | |- | ||
|13:11 | |13:11 | ||
− | |We use subplot command to accomplish this. | + | |We use subplot command to accomplish this. So we can type subplot within brackets 2 comma 1 comma 1. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 551: | Line 541: | ||
|- | |- | ||
|19:15 | |19:15 | ||
− | | Use the figure command. | + | | Use the figure command. Use the legend command. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
|19:18 | |19:18 | ||
− | | | + | |Switch between the plots and perform some operations on each of them like saving the plots. |
|- | |- |
Latest revision as of 10:38, 27 March 2017
Time | Narration |
00:00 | Hello friends and Welcome to this spoken tutorial on "Multiple plots". |
00:05 | At the end of this tutorial, you will be able to, draw multiple plots which are overlaid.
use the figure command. use the legend command switch between the plots and perform some operations on each of them like saving the plots. Create and switch between subplots |
00:25 | So, before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Embellishing a plot" and "Saving plots". |
00:35 | To begin with let us start ipython with pylab, by typing ipython space hyphen pylab on the terminal. |
00:51 | Let us first create set of points for our plot. |
00:54 | For this we will use the command called linspace |
00:56 | So type x is equal to linspace within brackets 0,50,10. |
01:07 | Linspace command creates 10 points in the interval between 0 and 50 both inclusive. We assign these values to a variable called x. |
01:17 | Now let us draw a simple sine plot using these points |
01:20 | So type plot within brackets x comma sin of x and hit Enter. |
01:31 | Oh! wait! |
01:33 | Is that a good sine plot? |
01:37 | Does a sine plot actually look like that? |
01:40 | We know that a sine plot is a smooth curve. |
01:43 | Is it not? What really caused this? |
01:47 | A small investigation on linspace tells us that we chose too few points in a large interval between 0 and 50 for the curve to be smooth. |
01:59 | This also indicates that the plot command actually plots the set of points given by x and sin(x) and it doesn't plot the analytical function itself rather it plots the points given by Analytical functions. |
02:14 | So now let us use linspace again to get 500 points between 0 and 100 and draw the sine plot again. |
02:22 | Now we see a sine plot with a smooth curve. |
02:26 | So type y is equal to linspace within brackets 0,50,500. Then type plot within brackets y,sin(y). |
02:48 | If we carefully notice we also have two plots now one overlaid upon another. |
02:55 | In pylab, by default all the plots are overlaid. |
02:58 | Since we have two plots now overlaid upon each other we would like to have a way to indicate what each plot represents to distinguish between them. |
03:06 | This is accomplished using legends. |
03:11 | Equivalently, the legend command does this for us. |
03:18 | The legend command takes a single list of parameters where each parameter is the text indicating the plots in the order of their serial number. |
03:25 | So we can type on the terminal legend within brackets and in square brackets in single quotes sin of x comma another single quotes sin of y. |
03:44 | Now we can see the legends being displayed for the respective sine and cosine plots on the plot area. |
03:52 | We have learnt quite a lot of things now, so let us take up an exercise. |
03:56 | Pause the video here,do the exercise then resume the video. |
04:02 | Draw a two plots overlaid upon each other, with the first plot being a parabola of the form y is equal to 4into x squared and the second being a straight line of the form y is equalto 2x plus 3 in the interval -5 to 5. |
04:19 | Use colors to differentiate between the plots and use legends to indicate what each plot is doing. |
04:26 | So now,Switch to the terminal for solution. We can obtain the two plots in different colors using the following commands |
04:33 | So type x is equal to linspace within brackets -5 comma 5 comma 100. |
04:42 | Then type plot within brackets x, 4 star within brackets x star x,within single quotes 'b'. |
04:53 | Then again plot within brackets x, 2 star x plus 3,within single quotes 'g'. |
05:05 | Now we can use the legend command as legend within brackets and square brackets within single quotes 'Parabola',in another single quotes 'Straight Line'. |
05:31 | Or we can also just give the equations of the plot like you can type legend within brackets in square brackets y = 4 into x ^ 2 comma y is equal to 2x plus 3. |
05:49 | We now know how to draw multiple plots and use legends to indicate which plot represents what function, but we would like to have more control over the plots we draw. |
05:59 | Like switch between them, perform some operations or labelling them individually and so on. |
06:06 | Let us see how to accomplish this. |
06:09 | But before we move on, let us clear our screen. So type clf then closing brackets. |
06:16 | To accomplish more control over individual plots we use the figure command. So for that you can type in terminal |
06:23 | x is equal to linspace within brackets 0 comma 50,500. |
06:29 | Then type figure within brackets 1. |
06:34 | Then type plot within brackets x comma sin(x) comma within single quotes 'b' and then figure(2).Then now you can type figure (2). |
06:59 | Then type plot within bracket x comma cos(x) comma within single quotes g |
07:33 | Now we have two plots, a sine plot and a cosine plot in two different figures. |
07:39 | The figure command takes an integer as an argument which is the serial number of the plot. |
07:43 | This selects the corresponding plot. |
07:45 | All the plot commands we run hereafter are applied on the selected plot. |
07:51 | In this figure 1 is the sine plot and figure 2 is the cosine plot. |
08:00 | For example,we can save each plot separately |
08:07 | We also titled our first plot as 'sin(y)' which we did not do for the second plot. |
08:14 | So for that we can type on the terminal savefig within brackets in single quotes slash home slash user slash cosine dot png. |
08:35 | Then figure(1). |
08:42 | Then title within brackets and single quotes sin(y). |
08:55 | Then savefig within brackets and single quotes slash home slash fossee slash sine dot png |
09:10 | In the cosine command also you can use fossee instead of home user. |
09:17 | Let us attempt another exercise problem. |
09:22 | Pause here,try to solve the problem and resume the video. |
09:26 | Draw a line of the form y is equal to x as one figure and another line of the form y is equal to 2x plus 3. |
09:34 | Switch back to the first figure,annotate the x and y intercepts. |
09:39 | Now switch to the second figure and annotate its x and y intercepts. |
09:43 | Save each of them. |
09:46 | Now, Switch to the terminal for solution. |
09:49 | To solve this problem we should first create the first figure using the figure command. |
09:53 | Before that, let us first run clf command to make sure all the previous plots are cleared |
10:00 | Then type clf(). |
10:03 | Then type figure 1. |
10:07 | Then type x is equal to linspace in brackets -5 comma 5 comma 500. |
10:14 | Then plot x comma x. |
10:22 | Now use the figure command to create second plotting area and plot the figure |
10:27 | So type figure 2. |
10:29 | Then type plot within brackets x comma within brackets 2 star x plus 3. |
10:52 | Now to switch between the figures we can use figure command. |
10:56 | So let us now switch to figure 1. |
11:00 | We are asked to annotate x and y intercepts of the figure 1, but since figure 1 passes through origin,this means, we will have to annotate the origin. |
11:10 | We will annotate the intercepts for the second figure and save them as follows. |
11:14 | So we can type on the terminal figure 1. |
11:21 | Then type annotate within brackets origin comma xy is equal to 0 point 0 comma 0 point 0. |
11:30 | Then figure 2. |
11:38 | So figure 2 . Then annotate within brackets x hyphen intercept comma xy is equal to within brackets 0 comma 3. |
12:05 | Then annotate y hyphen intercept comma xy is equal to within brackets 0 comma -1.5. |
12:18 | You can save figure by typing savefig within brackets slash home slash fossee slash plot2 dot png. |
12:28 | Then figure 1 and then for saving it we can type savefig within brackets in single quotes slash home slash fossee slash plot1 dot png. |
12:52 | At times we run into situations where we want to compare two plots and in such cases we want to draw both the plots in the same plotting area. |
13:00 | The situation is such that the two plots have different regular axes which means we cannot draw overlaid plots. |
13:07 | In such cases we can draw subplots. |
13:11 | We use subplot command to accomplish this. So we can type subplot within brackets 2 comma 1 comma 1. |
13:26 | As we can see subplot command takes three arguments, the first being the number of rows of subplots that must be created,in this case we have 2 as the first argument so it splits the plotting area horizontally for two subplots. |
13:44 | The second argument specifies the number of columns of subplots that must be created. |
13:49 | We passed 1 as the argument so the plotting area won't be split vertically and the last argument specifies what subplot must be created now in order of the serial number. |
14:02 | In this case we passed 1 as the argument, so the first subplot that is top half is created. |
14:08 | If we execute the subplot command as you can type on the terminal subplot within brackets 2 comma 1 comma 2. |
14:19 | The lower subplot is created now. |
14:23 | we can draw plots in each of the subplot area using the plot command. |
14:26 | So we can type on the terminal x=linspace within brackets 0 comma 50 comma 500. |
14:36 | Then plot within brackets x comma cos(x). |
14:46 | Then subplot 2 comma 1 comma 1. |
14:54 | Then y is equal to linspace within brackets 0 comma 5 comma 100. |
15:01 | Then plot y comma y star star 2. |
15:10 | This created two plots one in each of the subplot area. |
15:15 | The top subplot holds a parabola and the bottom subplot holds a cosine curve. |
15:22 | As seen here we can use subplot command to switch between the subplots as well, but we have to use the same arguments as we used to create that subplot, otherwise the previous subplot at that place will be automatically erased. |
15:34 | It is clear from the two subplots that both have different regular axes. |
15:40 | For the cosine plot x-axis varies from 0 to 100 and y-axis varies from 0 to 1 where as for the parabolic plot the x-axis varies from 0 to 10 and y-axis varies from 0 to 100. |
15:54 | Let us try one more exercise. |
15:56 | Pause the video here, try out the following exercise and resume the video. |
16:01 | We know that the Pressure, Volume and Temperatures are held by the equation PV = nRT where nR is a constant. |
16:10 | Let us assume nR =0.01 Joules/Kelvin and T = 200K. |
16:19 | V can be in the range from 21cc to 100cc. |
16:27 | Draw two different plots as subplots, one being the Pressure versus Volume plot and the other being Pressure versus Temperature plot. |
16:37 | Switch to the terminal for solution. |
16:41 | To start with, we have been given the range of Volume using which we can define the variable V |
16:48 | V is equal to linspace within brackets 21 comma 100 comma 500. |
16:58 | Now we can create first subplot and draw Pressure versus Volume graph using this V. |
17:05 | We know that nRT is a constant which is equal to 2.0 since nR = 0.01 Joules per Kelvin and T = 200 Kelvin |
17:15 | So we can type on the terminal subplot 2 comma 1 comma 1. |
17:23 | Then plot V comma 2 point 0 slash V. |
17:33 | Now we can create the second subplot and draw the Pressure versus Temperature plot as follows |
17:39 | subplot 2 comma 1 comma 2 |
17:44 | Then type plot within brackets 200 comma 2.0 slash V. |
18:04 | Unfortunately we have an error now, telling x and y dimensions don't match. |
18:10 | This is because our V contains a set of values as returned by linspace and hence 2.0 slash V which is the pressure also contains a set of values. |
18:20 | But the first argument to the plot command is a single value. |
18:23 | So to plot this data we need to create as many points as there are in Pressure or Volume data for Temperature , all having the same value. |
18:34 | Hence we do this, by typing on the terminal T is equal to linspace within brackets 200 comma 200 comma 500. |
18:48 | We now have 500 values in T each with the value 200 Kelvin. |
18:54 | Plotting this data, we get the required plot |
18:56 | So for that we can type plot within brackets T comma 2 point 0 slash V. |
19:07 | So This brings us to the end of this tutorial. |
19:11 | In this tutorial,we have learnt to, Draw multiple plots which are overlaid. |
19:15 | Use the figure command. Use the legend command. |
19:18 | Switch between the plots and perform some operations on each of them like saving the plots. |
19:24 | Create subplots to switch between them. |
19:28 | Here are some self assessment questions for you to solve |
19:33 | 1. What command is used to get individual plots separately?. |
19:38 | 2. Which of the following is correct. subplot(numRows, numCols, plotNum) , subplot(numRows, numCols) , subplot(numCols, numRows) |
19:59 | Now we will look at the answers, |
20:02 | 1. The command "figure()" can get us the individual plots separately. |
20:09 | 2. The subplot command takes three arguments namely the number of rows followed by the number of columns and the plot number. |
20:17 | Hence the first option is correct. |
20:19 | i.e. subplot(numrows,numCols,plotNum) |
20:25 | So we Hope you have enjoyed and found it useful. |