Application Center - Maplesoft

App Preview:

Constructing Advanced Plots

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

Image 

Constructing Advanced Plots 

? Maplesoft, a division of Waterloo Maple Inc., 2006 

Introduction 

Maple offers many different tools for creating plots. The context-sensitive menu allows you to create a plot with just a few clicks. The Plot Builder (Demonstrated in an earlier tips and techniques) allows you to specify different options for your plots. Maple's plotting packages allow you to create complex plots made up by many parts. This Tips and Techniques will demonstrate commands from Maple's plotting packages.  

 

Demonstrated below are ways to combine different plots; describe your plots with legends; customize your plots; add different shapes and text to your plots; and create animated plots. 

Plot 

Combining multiple plots 

When working with Maple's plotting commands, you will often want to have multiple plots appear together. You can accomplish this when plotting expressions by dragging one or more expressions onto a plot. When you wish to have the plot generated using Maple input or wish to use shapes such as those explained later, you can use plots[display] . This command will take all the plots given in a list and display them together in a single plot.  

 

To demonstrate, we'll plot, sin(x) and cos(x) together. First, assign each plot to a variable. Use a colon at the end of each line to suppress the output of the plot structure. 

 

plotA := plot(sin(x), x = -Pi .. Pi); -1 

plotB := plot(cos(x), x = -Pi .. Pi); -1 

 

Now use plots[display] to output both plots into one plot. Just pass a list of plots in, and plots[display] will combine all of the given plots 

 

plots[display]([plotA, plotB]) 

Plot
 

 

And there we have it, two plots combined together. Adding more plots it as easy as adding another element to the list given to the command. 

Adding a legend 

In our first plot we can clearly see both the sine and cosine curves. However it would be easier to figure out which one is which if there was a legend on the plot. Any options that you specify in the original plot commands will get passed on to the display command. So let's modify the original plots and add a legend and color to the plots. 

 

plotA := plot(sin(x), x = -Pi .. Pi, color = green, legend =  

plotB := plot(cos(x), x = -Pi .. Pi, color = navy, legend =  

plots[display]([plotA, plotB]) 

Plot
 

More Options 

You can also give plot options to the display command which will then be applied to all of the plots in the list. See ?plot,options for information on all of the options that can be used. 

 

Let's increase the thickness of the lines used. We will also add a title to the plot. 

 

plots[display]([plotA, plotB], thickness = 2, title =  

Plot
 

 

You will see that by adding the thickness option to plots[display], both lines had their thickness increased. If you want the lines to have different thickness you can define the thickness when the lines are created: 

 

plotA := plot(sin(x), x = -Pi .. Pi, color = green, legend =  

plotB := plot(cos(x), x = -Pi .. Pi, color = navy, legend =  

plots[display]([plotA, plotB]) 

Plot
 

 

Adding shapes to your plots 

The plottools package offers many different shapes that you can use within your plots. The shapes it generates can be either two or three dimensional. Several functions s can create both 2-D and 3-D plots. With these functions, if you give the function a position with two coordinates, you will get a 2-D plot and if you give three coordinates, you will get a 3-D plot. 

 

To demonstrate, we will create several lines by giving the start and end points. 

 

lines := plottools[line]([-10, -15], [5, 10], color = green), plottools[line]([5, 10], [30, 20], color = red), plottools[line]([30, 20], [-10, -15], color = magenta); -1
lines := plottools[line]([-10, -15], [5, 10], color = green), plottools[line]([5, 10], [30, 20], color = red), plottools[line]([30, 20], [-10, -15], color = magenta); -1
lines := plottools[line]([-10, -15], [5, 10], color = green), plottools[line]([5, 10], [30, 20], color = red), plottools[line]([30, 20], [-10, -15], color = magenta); -1
 

plots[display]([lines]); 1 

Plot
 

Now, we can do the same thing in 3-D 

 

lines := plottools[line]([-10, -15, -3], [5, 10, 10], color = green), plottools[line]([5, 10, 10], [30, 20, 15], color = red), plottools[line]([30, 20, 15], [-10, -15, -3], color = magenta); -1
lines := plottools[line]([-10, -15, -3], [5, 10, 10], color = green), plottools[line]([5, 10, 10], [30, 20, 15], color = red), plottools[line]([30, 20, 15], [-10, -15, -3], color = magenta); -1
lines := plottools[line]([-10, -15, -3], [5, 10, 10], color = green), plottools[line]([5, 10, 10], [30, 20, 15], color = red), plottools[line]([30, 20, 15], [-10, -15, -3], color = magenta); -1
 

plots[display]([lines], axes = boxed, orientation = ([200, 120])) 

Plot
 

 

As you can see the only difference is the extra dimension given for each coordinate. 

 

Now let's demonstrate some of the other shapes that you can create. 

You can create a sphere , giving the center of the sphere and its radius. 

 

theSphere := plottools[sphere]([0, 0, 0], 1.5); -1 

plots[display](theSphere) 

Plot
 

 

 

A disk , again with the center and the radius 

 

theDisk := plottools[disk]([10, 0, 5], 5, color = red); -1 

plots[display](theDisk) 

Plot
 

 

A cylinder with the center, radius, and height 

 

theCylinder := plottools[cylinder]([1, 1, 1], 1, 3); -1 

plots[display](theCylinder) 

Plot
 

 

 

Or even a dodecahedron with the center and scale 

 

theDodecahedron := plottools[dodecahedron]([0, 0, 0], .8); -1 

plots[display](theDodecahedron, scaling = constrained) 

Plot
 

 

Including text 

plots[textplot] makes it easy to place text anywhere you would like on your plot. You can give textplot a list of lists describing the text that you would like to appear. The parameters are: the coordinates for the text and the actual text. As an example, we will label an intersection of the sine and cosine curves from our first example. 

 

Find the intersection point 

solve(sin(x) = cos(x), x) 

1/4*Pi (3.1)
 

And create the text. We'll offset the x value a bit so that the text does not interfere with the plot. We also use the align option to specify that we want the text aligned to the right hand side of the given point. 

 

text := plots[textplot]([1/4*Pi+.2, sin(1/4*Pi),  

plots[display]([plotA, plotB, text]) 

Plot
 

 

Now use textplot3d to see that we can place the text anywhere we would like in 3-D. Below we will label several points on a sphere. 

 

theSphere := plottools[sphere]([0, 0, 0], .6); -1 

theText := plots[textplot3d]([[1, 0, 0,  

plots[display]([theSphere, theText], axes = boxed, view = ([-1.1 .. 1.1, -1.1 .. 1.1, -1.1 .. 1.1]), orientation = ([40, 50])) 

Plot
 

 

Try rotating the above plot by clicking and dragging the mouse, you'll see that the text is in 3-D. 

 

Using Arrows 

Often you may wish to display an arrow that can be used to point out a specific part of a plot. This can be done with plots[arrow] . Arrows can be in two or three dimensions.  The arrow can take one or two arguments. If one argument is given, it will create an arrow from the origin with the first argument taken as a direction vector. If two arguments are given, the first is the start point, the second is the direction vector. 

 

First, we will create a 2-D example with an arrow pointing to the maximum of the sine curve. 

 

arrow2d := plots[arrow]([-1, 1], [1, 0.]); -1 

cosPlot := plot(cos(x), x = -3 .. 6); -1 

text := plots[textplot]([-1.1, 1,  

plots[display](cosPlot, text, arrow2d) 

Plot
 

 

To change the plot to 3-D, just add a third dimension to each coordinate. For an example of 3-D arrows, see the demonstration in the last section of this document. 

 

Animating your plots 

It is simple to create animations in Maple using the plots[animate] command.  

 

The animate command takes at least three parameters. The first is the name of the plot function you wish to use. The second is a list of parameters that you would have given the plot function. The last is a variable and range you want the animation to travel between. 

 

As an example, lets show a simple 3-D plot. To create the animation, we will first create the plot we wish to animate. 

 

plot3d(x^2*y^2, x = -10 .. 10, y = -10 .. 10) 

Plot
 

 

 

Now we create the animated version. The first argument of the animate command is the plot command used to create each frame. The second argument is a list containing the arguments to that plot command.  

 

plots[animate](plot3d, [x^2*y^2*t, x = -10 .. 10, y = -10 .. 10], t = 0 .. 1) 

Plot
 

 

To see the animation, click on the plot, and then click on the play button that appears on your toolbar. 

Demonstration: Cross product 

As a demonstration of putting together a number of plot tools, we will recreate this image taken from the Wikimedia Commons 

 

Image 

First, we will create the arrows. There are five arrows all of which start at the origin, so we can use the one parameter version of plots[arrow] 

 

arrows := plots[arrow]([0, -1, 0], color = green), plots[arrow]([0, .3, 0], color = red, width = 0.5e-1), plots[arrow]([0, .3, 0], [0, .8, 0], color = blue), plots[arrow]([1, 0, 1], color = maroon), p...
arrows := plots[arrow]([0, -1, 0], color = green), plots[arrow]([0, .3, 0], color = red, width = 0.5e-1), plots[arrow]([0, .3, 0], [0, .8, 0], color = blue), plots[arrow]([1, 0, 1], color = maroon), p...
arrows := plots[arrow]([0, -1, 0], color = green), plots[arrow]([0, .3, 0], color = red, width = 0.5e-1), plots[arrow]([0, .3, 0], [0, .8, 0], color = blue), plots[arrow]([1, 0, 1], color = maroon), p...
arrows := plots[arrow]([0, -1, 0], color = green), plots[arrow]([0, .3, 0], color = red, width = 0.5e-1), plots[arrow]([0, .3, 0], [0, .8, 0], color = blue), plots[arrow]([1, 0, 1], color = maroon), p...
arrows := plots[arrow]([0, -1, 0], color = green), plots[arrow]([0, .3, 0], color = red, width = 0.5e-1), plots[arrow]([0, .3, 0], [0, .8, 0], color = blue), plots[arrow]([1, 0, 1], color = maroon), p...
 

plots[display](arrows, scaling = constrained, orientation = ([-90, -20])) 

Plot
 

You can see that we did use the two parameter version for the blue arrows. This is is because if we were to start both the blue and red arrows at the origin, you would only be able to see the blue arrow. Since it is longer, it would cover up the red arrow. 

 

Now let's add the lines. We define some values that will be the same for all of the lines first. 

 

lineY := .15; -1 

lineLength := .2; -1 

lineOptions := thickness = 3, color = black; -1 

 

And then the actual lines 

 

lines := plottools[line]([0, lineY, 0], [lineLength, lineY, lineLength], lineOptions), plottools[line]([0, lineY, 0], [lineLength, lineY, -lineLength], lineOptions), plottools[line]([lineLength, lineY...
lines := plottools[line]([0, lineY, 0], [lineLength, lineY, lineLength], lineOptions), plottools[line]([0, lineY, 0], [lineLength, lineY, -lineLength], lineOptions), plottools[line]([lineLength, lineY...
lines := plottools[line]([0, lineY, 0], [lineLength, lineY, lineLength], lineOptions), plottools[line]([0, lineY, 0], [lineLength, lineY, -lineLength], lineOptions), plottools[line]([lineLength, lineY...
lines := plottools[line]([0, lineY, 0], [lineLength, lineY, lineLength], lineOptions), plottools[line]([0, lineY, 0], [lineLength, lineY, -lineLength], lineOptions), plottools[line]([lineLength, lineY...
lines := plottools[line]([0, lineY, 0], [lineLength, lineY, lineLength], lineOptions), plottools[line]([0, lineY, 0], [lineLength, lineY, -lineLength], lineOptions), plottools[line]([lineLength, lineY...
 

plots[display]([arrows, lines], scaling = constrained, orientation = ([-90, -30])); 1 

Plot
 

 

Next, add the arc. plottools[arc] , takes three parameters, the center of the circle, the radius of the circle, and a range between the starting angle of the arc (in radians) and the ending angle of the arc. 

 

theArc := plottools[arc]([0, 0], .3, -1/4*Pi .. 1/4*Pi, thickness = 2); -1 

plots[display]([arrows, lines, theArc], scaling = constrained, orientation = ([-90, -30])) 

Error, (in plots/display) cannot display 2-D and 3-D plots together
 

 

Ok, we have a problem. plottools[arc] only generates 2-D plots. What we need to do is convert the 2-D plot of an arc into a 3-D plot. First, let's look at the 2-D plot. 

 

plots[display](theArc); 1 

Plot
 

 

Now we will use plottools[transform] to convert the 2-D plot into a 3-D plot. transform takes one parameter, f, which is a procedure that maps two coordinates into three coordinates. The function returns a procedure that we can call to convert a plot structure. For our example,we'll map x to the x coordinate in 3-D. Set the y to the 3-D z coordinate and set the 3-D y coordinate to zero. 

 

f := plottools[transform](proc (x, y) options operator, arrow; [x, 0, y] end proc); -1 

 

Do the transformation 

 

theArc3d := f(theArc); -1 

 

And display it. 

 

plots[display](theArc3d); 1 

Plot
 

 

 

As you can see, we got a 3-D plot. So now we can add it to our plot 

 

plots[display]([arrows, lines, theArc3d], scaling = constrained, orientation = ([-90, -30])) 

Plot
 

Finally, we'll create the text that describes the plot. 

 

text := plots[textplot3d]([[.4, 0, 0,
text := plots[textplot3d]([[.4, 0, 0,
text := plots[textplot3d]([[.4, 0, 0,
text := plots[textplot3d]([[.4, 0, 0,
text := plots[textplot3d]([[.4, 0, 0,
text := plots[textplot3d]([[.4, 0, 0,
text := plots[textplot3d]([[.4, 0, 0,
 

plots[display]([arrows, lines, theArc3d, text], scaling = constrained, orientation = ([-90, -30])) 

Plot
 

 

There we have it - our plot. And unlike the original, this plot can be easily modified, added to, and because it is a 3-D object, it can be rotated. This document demonstrates the combination of several of Maple's plotting tools to create an interactive and attractive plot. We leave it as an exercise to the reader to change the colors of the letters to match up with the colors of the arrows. 

 

Legal Notice: The copyright for this application is owned by Maplesoft. The application is intended to demonstrate the use of Maple to solve a particular problem. It has been made available for product evaluation purposes only and may not be used in any other context without the express permission of Maplesoft.  
 

Image