1
00:00:00 --> 00:00:04
Hello friends and Welcome to this spoken tutorial on "Multiple plots".
2
00:00:05 --> 00:00:24
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
3
00:00:25 --> 00:00:34
So, before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Embellishing a plot" and "Saving plots".
4
00:00:35 --> 00:00:50
To begin with let us start ipython with pylab, by typing ipython space hyphen pylab on the terminal.
5
00:00:51 --> 00:00:53
Let us first create set of points for our plot.
6
00:00:54 --> 00:00:55
For this we will use the command called linspace
7
00:00:56 --> 00:01:06
So type x is equal to linspace within brackets 0,50,10.
8
00:01:07 --> 00:01:16
Linspace command creates 10 points in the interval between 0 and 50 both inclusive. We assign these values to a variable called x.
9
00:01:17 --> 00:01:19
Now let us draw a simple sine plot using these points
10
00:01:20 --> 00:01:30
So type plot within brackets x comma sin of x and hit Enter.
11
00:01:31 --> 00:01:32
Oh! wait!
12
00:01:33 --> 00:01:36
Is that a good sine plot?
13
00:01:37 --> 00:01:39
Does a sine plot actually look like that?
14
00:01:40 --> 00:01:42
We know that a sine plot is a smooth curve.
15
00:01:43 --> 00:01:46
Is it not? What really caused this?
16
00:01:47 --> 00:01:58
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.
17
00:01:59 --> 00:02:13
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.
18
00:02:14 --> 00:02:21
So now let us use linspace again to get 500 points between 0 and 100 and draw the sine plot again.
19
00:02:22 --> 00:02:25
Now we see a sine plot with a smooth curve.
20
00:02:26 --> 00:02:47
So type y is equal to linspace within brackets 0,50,500. Then type plot within brackets y,sin(y).
21
00:02:48 --> 00:02:54
If we carefully notice we also have two plots now one overlaid upon another.
22
00:02:55 --> 00:02:57
In pylab, by default all the plots are overlaid.
23
00:02:58 --> 00:03:05
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.
24
00:03:06 --> 00:03:10
This is accomplished using legends.
25
00:03:11 --> 00:03:17
Equivalently, the legend command does this for us.
26
00:03:18 --> 00:03:24
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.
27
00:03:25 --> 00:03:43
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.
28
00:03:44 --> 00:03:51
Now we can see the legends being displayed for the respective sine and cosine plots on the plot area.
29
00:03:52 --> 00:03:55
We have learnt quite a lot of things now, so let us take up an exercise.
30
00:03:56 --> 00:04:01
Pause the video here,do the exercise then resume the video.
31
00:04:02 --> 00:04:18
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.
32
00:04:19 --> 00:04:25
Use colors to differentiate between the plots and use legends to indicate what each plot is doing.
33
00:04:26 --> 00:04:32
So now,Switch to the terminal for solution. We can obtain the two plots in different colors using the following commands
34
00:04:33 --> 00:04:41
So type x is equal to linspace within brackets -5 comma 5 comma 100.
35
00:04:42 --> 00:04:52
Then type plot within brackets x, 4 star within brackets x star x,within single quotes 'b'.
36
00:04:53 --> 00:05:04
Then again plot within brackets x, 2 star x plus 3,within single quotes 'g'.
37
00:05:05 --> 00:05:30
Now we can use the legend command as legend within brackets and square brackets within single quotes 'Parabola',in another single quotes 'Straight Line'.
38
00:05:31 --> 00:05:48
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.
39
00:05:49 --> 00:05:58
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.
40
00:05:59 --> 00:06:05
Like switch between them, perform some operations or labelling them individually and so on.
41
00:06:06 --> 00:06:08
Let us see how to accomplish this.
42
00:06:09 --> 00:06:15
But before we move on, let us clear our screen. So type clf then closing brackets.
43
00:06:16 --> 00:06:22
To accomplish more control over individual plots we use the figure command. So for that you can type in terminal
44
00:06:23 --> 00:06:28
x is equal to linspace within brackets 0 comma 50,500.
45
00:06:29 --> 00:06:33
Then type figure within brackets 1.
46
00:06:34 --> 00:06:58
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).
47
00:06:59 --> 00:07:32
Then type plot within bracket x comma cos(x) comma within single quotes g
48
00:07:33 --> 00:07:38
Now we have two plots, a sine plot and a cosine plot in two different figures.
49
00:07:39 --> 00:07:42
The figure command takes an integer as an argument which is the serial number of the plot.
50
00:07:43 --> 00:07:44
This selects the corresponding plot.
51
00:07:45 --> 00:07:50
All the plot commands we run hereafter are applied on the selected plot.
52
00:07:51 --> 00:07:59
In this figure 1 is the sine plot and figure 2 is the cosine plot.
53
00:08:00 --> 00:08:06
For example,we can save each plot separately
54
00:08:07 --> 00:08:13
We also titled our first plot as 'sin(y)' which we did not do for the second plot.
55
00:08:14 --> 00:08:34
So for that we can type on the terminal savefig within brackets in single quotes slash home slash user slash cosine dot png.
56
00:08:35 --> 00:08:41
Then figure(1).
57
00:08:42 --> 00:08:54
Then title within brackets and single quotes sin(y).
58
00:08:55 --> 00:09:09
Then savefig within brackets and single quotes slash home slash fossee slash sine dot png
59
00:09:10 --> 00:09:16
In the cosine command also you can use fossee instead of home user.
60
00:09:17 --> 00:09:21
Let us attempt another exercise problem.
61
00:09:22 --> 00:09:25
Pause here,try to solve the problem and resume the video.
62
00:09:26 --> 00:09:33
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.
63
00:09:34 --> 00:09:38
Switch back to the first figure,annotate the x and y intercepts.
64
00:09:39 --> 00:09:42
Now switch to the second figure and annotate its x and y intercepts.
65
00:09:43 --> 00:09:45
Save each of them.
66
00:09:46 --> 00:09:48
Now, Switch to the terminal for solution.
67
00:09:49 --> 00:09:52
To solve this problem we should first create the first figure using the figure command.
68
00:09:53 --> 00:09:59
Before that, let us first run clf command to make sure all the previous plots are cleared
69
00:10:00 --> 00:10:02
Then type clf().
70
00:10:03 --> 00:10:06
Then type figure 1.
71
00:10:07 --> 00:10:13
Then type x is equal to linspace in brackets -5 comma 5 comma 500.
72
00:10:14 --> 00:10:21
Then plot x comma x.
73
00:10:22 --> 00:10:26
Now use the figure command to create second plotting area and plot the figure
74
00:10:27 --> 00:10:28
So type figure 2.
75
00:10:29 --> 00:10:51
Then type plot within brackets x comma within brackets 2 star x plus 3.
76
00:10:52 --> 00:10:55
Now to switch between the figures we can use figure command.
77
00:10:56 --> 00:10:59
So let us now switch to figure 1.
78
00:11:00 --> 00:11:09
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.
79
00:11:10 --> 00:11:13
We will annotate the intercepts for the second figure and save them as follows.
80
00:11:14 --> 00:11:20
So we can type on the terminal figure 1.
81
00:11:21 --> 00:11:29
Then type annotate within brackets origin comma xy is equal to 0 point 0 comma 0 point 0.
82
00:11:30 --> 00:11:37
Then figure 2.
83
00:11:38 --> 00:12:04
So figure 2 . Then annotate within brackets x hyphen intercept comma xy is equal to within brackets 0 comma 3.
84
00:12:05 --> 00:12:17
Then annotate y hyphen intercept comma xy is equal to within brackets 0 comma -1.5.
85
00:12:18 --> 00:12:27
You can save figure by typing savefig within brackets slash home slash fossee slash plot2 dot png.
86
00:12:28 --> 00:12:51
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.
87
00:12:52 --> 00:12:59
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.
88
00:13:00 --> 00:13:06
The situation is such that the two plots have different regular axes which means we cannot draw overlaid plots.
89
00:13:07 --> 00:13:10
In such cases we can draw subplots.
90
00:13:11 --> 00:13:25
We use subplot command to accomplish this. So we can type subplot within brackets 2 comma 1 comma 1.
91
00:13:26 --> 00:13:43
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.
92
00:13:44 --> 00:13:48
The second argument specifies the number of columns of subplots that must be created.
93
00:13:49 --> 00:14:01
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.
94
00:14:02 --> 00:14:07
In this case we passed 1 as the argument, so the first subplot that is top half is created.
95
00:14:08 --> 00:14:18
If we execute the subplot command as you can type on the terminal subplot within brackets 2 comma 1 comma 2.
96
00:14:19 --> 00:14:22
The lower subplot is created now.
97
00:14:23 --> 00:14:25
we can draw plots in each of the subplot area using the plot command.
98
00:14:26 --> 00:14:35
So we can type on the terminal x=linspace within brackets 0 comma 50 comma 500.
99
00:14:36 --> 00:14:45
Then plot within brackets x comma cos(x).
100
00:14:46 --> 00:14:53
Then subplot 2 comma 1 comma 1.
101
00:14:54 --> 00:15:00
Then y is equal to linspace within brackets 0 comma 5 comma 100.
102
00:15:01 --> 00:15:09
Then plot y comma y star star 2.
103
00:15:10 --> 00:15:14
This created two plots one in each of the subplot area.
104
00:15:15 --> 00:15:21
The top subplot holds a parabola and the bottom subplot holds a cosine curve.
105
00:15:22 --> 00:15:33
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.
106
00:15:34 --> 00:15:39
It is clear from the two subplots that both have different regular axes.
107
00:15:40 --> 00:15:53
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.
108
00:15:54 --> 00:15:55
Let us try one more exercise.
109
00:15:56 --> 00:16:00
Pause the video here, try out the following exercise and resume the video.
110
00:16:01 --> 00:16:09
We know that the Pressure, Volume and Temperatures are held by the equation PV = nRT where nR is a constant.
111
00:16:10 --> 00:16:18
Let us assume nR =0.01 Joules/Kelvin and T = 200K.
112
00:16:19 --> 00:16:26
V can be in the range from 21cc to 100cc.
113
00:16:27 --> 00:16:36
Draw two different plots as subplots, one being the Pressure versus Volume plot and the other being Pressure versus Temperature plot.
114
00:16:37 --> 00:16:40
Switch to the terminal for solution.
115
00:16:41 --> 00:16:47
To start with, we have been given the range of Volume using which we can define the variable V
116
00:16:48 --> 00:16:57
V is equal to linspace within brackets 21 comma 100 comma 500.
117
00:16:58 --> 00:17:04
Now we can create first subplot and draw Pressure versus Volume graph using this V.
118
00:17:05 --> 00:17:14
We know that nRT is a constant which is equal to 2.0 since nR = 0.01 Joules per Kelvin and T = 200 Kelvin
119
00:17:15 --> 00:17:22
So we can type on the terminal subplot 2 comma 1 comma 1.
120
00:17:23 --> 00:17:32
Then plot V comma 2 point 0 slash V.
121
00:17:33 --> 00:17:38
Now we can create the second subplot and draw the Pressure versus Temperature plot as follows
122
00:17:39 --> 00:17:43
subplot 2 comma 1 comma 2
123
00:17:44 --> 00:18:03
Then type plot within brackets 200 comma 2.0 slash V.
124
00:18:04 --> 00:18:09
Unfortunately we have an error now, telling x and y dimensions don't match.
125
00:18:10 --> 00:18:19
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.
126
00:18:20 --> 00:18:22
But the first argument to the plot command is a single value.
127
00:18:23 --> 00:18:33
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.
128
00:18:34 --> 00:18:47
Hence we do this, by typing on the terminal T is equal to linspace within brackets 200 comma 200 comma 500.
129
00:18:48 --> 00:18:53
We now have 500 values in T each with the value 200 Kelvin.
130
00:18:54 --> 00:18:55
Plotting this data, we get the required plot
131
00:18:56 --> 00:19:06
So for that we can type plot within brackets T comma 2 point 0 slash V.
132
00:19:07 --> 00:19:10
So This brings us to the end of this tutorial.
133
00:19:11 --> 00:19:14
In this tutorial,we have learnt to, Draw multiple plots which are overlaid.
134
00:19:15 --> 00:19:17
Use the figure command. Use the legend command.
135
00:19:18 --> 00:19:23
Switch between the plots and perform some operations on each of them like saving the plots.
136
00:19:24 --> 00:19:27
Create subplots to switch between them.
137
00:19:28 --> 00:19:32
Here are some self assessment questions for you to solve
138
00:19:33 --> 00:19:37
1. What command is used to get individual plots separately?.
139
00:19:38 --> 00:19:58
2. Which of the following is correct. subplot(numRows, numCols, plotNum) , subplot(numRows, numCols) , subplot(numCols, numRows)
140
00:19:59 --> 00:20:01
Now we will look at the answers,
141
00:20:02 --> 00:20:08
1. The command "figure()" can get us the individual plots separately.
142
00:20:09 --> 00:20:16
2. The subplot command takes three arguments namely the number of rows followed by the number of columns and the plot number.
143
00:20:17 --> 00:20:18
Hence the first option is correct.
144
00:20:19 --> 00:20:24
i.e. subplot(numrows,numCols,plotNum)
145
00:20:25 --> 00:20:30
So we Hope you have enjoyed and found it useful.