https://script.spoken-tutorial.org/index.php?title=Python/C2/Multiple-plots/English&feed=atom&action=historyPython/C2/Multiple-plots/English - Revision history2019-09-16T02:07:49ZRevision history for this page on the wikiMediaWiki 1.23.17https://script.spoken-tutorial.org/index.php?title=Python/C2/Multiple-plots/English&diff=932&oldid=prevPravin1389: moved Python/C2/Multiple-plots /English to Python/C2/Multiple-plots/English2012-12-02T06:30:39Z<p>moved <a href="/index.php/Python/C2/Multiple-plots_/English" class="mw-redirect" title="Python/C2/Multiple-plots /English">Python/C2/Multiple-plots /English</a> to <a href="/index.php/Python/C2/Multiple-plots/English" title="Python/C2/Multiple-plots/English">Python/C2/Multiple-plots/English</a></p>
<table class='diff diff-contentalign-left'>
<tr style='vertical-align: top;'>
<td colspan='1' style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black; text-align: center;">Revision as of 06:30, 2 December 2012</td>
</tr><tr><td colspan='2' style='text-align: center;'><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Pravin1389https://script.spoken-tutorial.org/index.php?title=Python/C2/Multiple-plots/English&diff=448&oldid=prevChandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1 containing title, name of the production team along with the logo of MHRD | Hello friends and Welcome to this spoken tutor…'2012-11-29T05:55:45Z<p>Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1 containing title, name of the production team along with the logo of MHRD | Hello friends and Welcome to this spoken tutor…'</p>
<p><b>New page</b></p><div>{| border=1<br />
!Visual Cue<br />
!Narration<br />
|-<br />
| Show Slide 1 <br />
<br />
containing title, name of the production team along with the logo of MHRD <br />
| Hello friends and Welcome to this spoken tutorial on "Multiple plots".<br />
<br />
|-<br />
| Show Slide 2 <br />
<br />
Learning objectives <br />
| At the end of this tutorial, you will be able to,<br />
<br />
# draw multiple plots which are overlaid.<br />
# use the figure command.<br />
# use the legend command<br />
# switch between the plots and perform some operations on each of them like saving the plots.<br />
# create and switch between subplots<br />
<br />
<br />
<br />
|-<br />
| Show Slide 3 <br />
<br />
pre-requisite <br />
<br />
Shift to terminal and start ipython -pylab <br />
<br />
ipython -pylab<br />
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Embellishing a plot" and "Saving plots".<br />
<br />
To begin with let us start ipython with pylab, by typing ipython -pylab on the terminal.<br />
<br />
|-<br />
| x = linspace(0, 50, 10)<br />
| Let us first create set of points for our plot. For this we will use the command called linspace<br />
<br />
|-<br />
| plot(x, sin(x))<br />
| Linspace command creates 10 points in the interval between 0 and 50 both inclusive. We assign these values to a variable called x.<br />
<br />
Now let us draw a simple sine plot using these points<br />
<br />
|-<br />
| Switch to the plot window <br />
| Oh! wait! Is that a good sine plot? Does a sine plot actually look like that? We know that a sine plot is a smooth curve. Is it not? What really caused this?<br />
<br />
|-<br />
| Pause for a while <br />
| 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. 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. So now let us use linspace again to get 500 points between 0 and 100 and draw the sine plot again.<br />
<br />
|-<br />
| Switch to terminal and type <br />
<br />
y = linspace(0, 50, 500)<br />
plot(y, sin(y))<br />
<br />
Switch to the plot window <br />
| Now we see a sine plot with a smooth curve, just as we wanted. If we carefully notice we also have two plots now one overlaid upon another. In pylab, by default all the plots are overlaid.<br />
<br />
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. This is accomplished using legends. Equivalently, the legend command does this for us.<br />
<br />
|-<br />
| Switch to terminal <br />
<br />
<nowiki>legend(['sin(x)', 'sin(y)'])</nowiki><br />
| 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.<br />
<br />
|-<br />
| Switch to plot window <br />
| Now we can see the legends being displayed for the respective sine and cosine plots on the plot area.<br />
<br />
|-<br />
| Show Slide 4<br />
<br />
Assessment 1 <br />
| We have learnt quite a lot of things now, so let us take up an exercise. Pause the video here,do the exercise and resume the video.<br />
<br />
Draw two plots overlaid upon each other, with the first plot being a parabola of the form y = 4(x ^ 2) and the second being a straight line of the form y = 2x + 3 in the interval -5 to 5. Use colors to differentiate between the plots and use legends to indicate what each plot is doing.<br />
<br />
|-<br />
| Pause for a while and continue from paused state <br />
<br />
x = linspace(-5, 5, 100)<br />
plot(x, 4 * (x * x), 'b')<br />
plot(x, (2 * x) + 3, 'g')<br />
| Switch to the terminal for solution. We can obtain the two plots in different colors using the following commands<br />
<br />
|-<br />
| <nowiki>legend(['Parabola', 'Straight Line'])</nowiki><br />
| Now we can use the legend command as<br />
<br />
|-<br />
| <nowiki>legend(['y = 4(x ^ 2)', 'y = 2x + 3'])</nowiki><br />
| Or we can also just give the equations of the plot<br />
<br />
|-<br />
| Switch to terminal <br />
<br />
clf()<br />
| 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. Like switch between them, perform some operations or labeling them individually and so on. Let us see how to accomplish this. But before we move on, let us clear our screen.<br />
<br />
|-<br />
| x = linspace(0, 50, 500)<br />
figure(1)<br />
plot(x, sin(x), 'b')<br />
figure(2)<br />
plot(x, cos(x), 'g')<br />
| To accomplishing more control over individual plots we use the figure command<br />
<br />
|-<br />
| Switch to plot window <br />
| Now we have two plots, a sine plot and a cosine plot in two different figures.<br />
<br />
|-<br />
| Show both plot window and terminal side by side <br />
| The figure command takes an integer as an argument which is the serial number of the plot. This selects the corresponding plot. All the plot commands we run hereafter are applied to the selected plot. In this example figure 1 is the sine plot and figure 2 is the cosine plot. For example,we can save each plot separately<br />
<br />
|-<br />
| Switch to terminal <br />
<br />
savefig('/home/user/cosine.png')<br />
figure(1)<br />
title('sin(y)')<br />
savefig('/home/user/sine.png')<br />
<br />
Have both plot window and ipython side by side <br />
| We also titled our first plot as 'sin(y)' which we did not do for the second plot.<br />
<br />
|-<br />
| Show Slide 5 <br />
<br />
Assignment 2 <br />
| Let us attempt another exercise problem. Pause here,try to solve the problem and resume the video.<br />
<br />
Draw a line of the form y = x as one figure and another line of the form y = 2x + 3. Switch back to the first figure,annotate the x and y intercepts. Now switch to the second figure and annotate its x and y intercepts. Save each of them.<br />
<br />
|-<br />
| Pause for a while and continue from the paused state <br />
<br />
clf()<br />
figure(1)<br />
x = linspace(-5, 5, 100)<br />
plot(x, x)<br />
| Switch to the terminal for solution. To solve this problem we should first create the first figure using the figure command. Before that, let us first run clf command to make sure all the previous plots are cleared<br />
<br />
|-<br />
| figure(2)<br />
plot(x, ((2 * x) + 3))<br />
| Now use the figure command to create second plotting area and plot the figure<br />
<br />
|-<br />
| figure(1)<br />
annotate('Origin', xy=(0.0, 0.0)<br />
figure(2)<br />
annotate('x-intercept', xy=(0, 3))<br />
annotate('y-intercept', xy=(0, -1.5))<br />
savefig('/home/fossee/plot2.png')<br />
figure(1)<br />
savefig('/home/fossee/plot1.png')<br />
| Now to switch between the figures we can use figure command. So let us now switch to figure 1. 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. We will annotate the intercepts for the second figure and save them as follows<br />
<br />
|-<br />
| Switch to terminal <br />
<br />
subplot(2, 1, 1)<br />
<br />
Have both plot window and ipython side by side <br />
| 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. The situation is such that the two plots have different regular axes which means we cannot draw overlaid plots. In such cases we can draw subplots.<br />
<br />
We use subplot command to accomplish this<br />
<br />
|-<br />
| Switch to terminal <br />
<br />
subplot(2, 1, 2)<br />
<br />
Switch to plot window <br />
| 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. The second argument specifies the number of columns of subplots that must be created. 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 the order of the serial number. In this case we passed 1 as the argument, so the first subplot that is top half is created. If we execute the subplot command as<br />
<br />
|-<br />
| Switch to terminal <br />
<br />
x = linspace(0, 50, 500)<br />
plot(x, cos(x))<br />
subplot(2, 1, 1)<br />
y = linspace(0, 5, 100)<br />
plot(y, y ** 2)<br />
| The lower subplot is created. Now we can draw plots in each of the subplot area using the plot command.<br />
<br />
|-<br />
| Have both plot window and ipython side by side <br />
| This created two plots one in each of the subplot area. The top subplot holds a parabola and the bottom subplot holds a cosine curve.<br />
<br />
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. It is clear from the two subplots that both have different regular axes. 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.<br />
<br />
|-<br />
| Show Slide 6 <br />
<br />
Assignment 3 <br />
| Let us try one more exercise. Pause the video here, try out the exercise and resume the video.<br />
<br />
We know that the Pressure, Volume and Temperatures are held by the equation PV = nRT where nR is a constant. Let us assume nR =0.01 Joules/Kelvin and T = 200K. V can be in the range from 21cc to 100cc. Draw two different plots as subplots, one being the Pressure versus Volume plot and the other being Pressure versus Temperature plot.<br />
<br />
|-<br />
| Pause for a while and continue <br />
<br />
V = linspace(21, 100, 500)<br />
| Switch to the terminal for solution. To start with, we have been given the range of Volume using which we can define the variable V<br />
<br />
|-<br />
| subplot(2, 1, 1)<br />
plot(V, 2.0/V)<br />
| Now we can create first subplot and draw Pressure versus Volume graph using this V. We know that nRT is a constant which is equal to 2.0 since nR = 0.01 Joules/Kelvin and T = 200 Kelvin<br />
<br />
|-<br />
| subplot(2, 1, 2)<br />
plot(200, 2.0/V)<br />
| Now we can create the second subplot and draw the Pressure versus Temperature plot as follows<br />
<br />
|-<br />
| T = linspace(200, 200, 500)<br />
| Unfortunately we have an error now, telling x and y dimensions don't match. This is because our V contains a set of values as returned by linspace and hence 2.0/V which is the pressure also contains a set of values. But the first argument to the plot command is a single value. So to plot this data we need to create as many points as there are in Pressure or Volume data for Temperature too, all having the same value. Hence we do this,<br />
<br />
|-<br />
| plot(T, 2.0/V)<br />
| We now have 500 values in T each with the value 200 Kelvin. Plotting this data, we get the required plot<br />
<br />
|-<br />
| Show Slide 7<br />
<br />
Summary slide <br />
| This brings us to the end of this tutorial. In this tutorial,we have learnt to,<br />
<br />
# Draw multiple plots which are overlaid.<br />
# Use the figure command.<br />
# Use the legend command.<br />
# Switch between the plots and perform some operations on each of them like saving the plots.<br />
# Create subplots and to switch between them.<br />
<br />
<br />
<br />
|-<br />
| Show Slide 8<br />
<br />
Self assessment questions slide<br />
| Here are some self assessment questions for you to solve<br />
<br />
# What command is used to get individual plots separately?.<br />
# Which of the following is correct.<br />
** subplot(numRows, numCols, plotNum)<br />
** subplot(numRows, numCols)<br />
** subplot(numCols, numRows)<br />
<br />
<br />
<br />
|-<br />
| Show Slide 9 <br />
<br />
Solution of self assessment questions on slide <br />
| And the answers,<br />
<br />
# The command "figure()" can get us the individual plots separately.<br />
# The subplot command takes three arguments namely the number of rows followed by the number of columns and the plot number. Hence the first option is correct.<br />
<br />
<br />
<br />
|-<br />
| Show Slide 10<br />
<br />
Acknowledgment <br />
| Hope you have enjoyed and found it useful. Thank you!</div>Chandrika