The Plot[...] Function: Associated Attributes & Operations

Basic Skills Continued

Original Author: Robert Kennedy

Improving Versatility with the Plot[...] Function

The purpose of this slide show is to demonstrate Mathematica’s Plot[...] function in as user-friendly a manner as possible.  This slide show will emphasize the plotting of discontinous, piecewise and other functions, as well as attributes that improve the visual presentation.  As you  read through this slide show you should immediately work your own examples to reinforce your reading.  Also, you should read through the relevant portions of the Documentation Center to improve your ability to read intelligibly this very powerful and effective resource.  Consequently, the description regarding individual elements is intentionally brief.

f[ x_ ]:= 2 x^3 - 4 x;

Show[
Plot[ f[ x ], {x,- 2, 2 }, Epilog->{ 
Line[{ { - 2, f[ -2 ] },{ - 2, 0} }],  
Line[{ { - 1.5, f[ - 2 ] }, { - 1.5, 0 } }],
Line[{ { - 1, f[ -1]}, { - 1, f[ - 1.5 ]} }],
Line[{ {- 0.5, f[ - 1 ] }, { - 0.5, 0 } }],
Line[{ {0, f[ -0.5]}, { 0, 0} }],
Line[{ { 0.5, 0}, { 0.5, f[ 0.5 ] } }],
Line[{ { 1, 0}, { 1, f[ 1 ] } }],
Line[{ { 1.5, f[ 1 ]}, { 1.5, f[ 1.5 ] } }],
Line[{ { 0.5, 0}, { 0.5, f[ 0.5 ] } }],
Line[{ { 2, 0}, { 2, f[ 1.5 ] } }]
 }
],
Plot[2 Floor[ 2 x ]^3 / 8 - 4 Floor[ 2 x ] / 2  , {x, - 2,2}, Filling-> Axis, PlotStyle->Black ],
PlotRange->{ {-2.2,2.2}, {-8.2, 8.2}}
]

Plotting Odd Root Functions

Due to Mathematica’s internal algorithm the following plot is incomplete.

Plot[ x^( 1/3 ), { x, -8, 8 }, PlotRange->{ - 2, 2 } ]

The domain of f(x)=(x^(1/3)) is all real numbers.  In order to complete the graph consider the following function, called the signum function.

sgn(x) = ({{{, ({{    1,, x>0}, {    0,, x=0}, {-1,, x<0}})}})

Multiplying the signum function with the absolute value of an odd root function will complete the graph.  Consider the following examples.

Plot[Abs[x]^(1/3)*Sign[x], {x, -2, 2}]

In case it is ambiguous, Abs[ x ] is the Mathematica input for the absolute value function.  Something similar is needed for reciprocal odd root functions.

Plot[ ( 1 / Abs[ x ]^( 1/3 ) )*Sign[ x ],{x,- 2, 2}, PlotRange->{ - 2, 2 } ]

Try Your Own Examples or Modify the Givens Below

Plotting Piecewise Functions

Consider the following piecewise function.

f(x) =({{{, ({{(x^2),, -2≤x<1}, {8-x,, x≥1}})}})

The following will plot the graph of f

Plot[ Piecewise[{ { x^2, -2≤ x< 1}, { - x +8, 1≤ x } } ], {x, -2, 4 }  ]

In Mathematica Piecewise[{ { branch 1}, { branch 2 }, ..., { branch n } }, everything else ] works as expected as long as you understand that the default for “everything else” is zero.  Hence, using a plotting range beyond the domain of the function plots y = 0 for “everything else.”  Consider

Plot[ Piecewise[{ { x^2, -2≤ x< 1}, { - x +8, 1≤ x } } ], {x, -6, 4 }  ]

Or with a slightly modified plotting range.

Plot[ Piecewise[{ { x^2, -2≤ x< 1}, { - x +8, 4≤ x } } ], {x, -6,6} ]

There are two possible ways to handle the previous challenges.  The first is to limit the plotting range and to specify the PlotRange.

Plot[ Piecewise[{ { x^2, -2≤ x< 1}, { - x +8, 1≤ x } } ], {x, -2,4 }, PlotRange->{ { -5, 7 }, { -1, 9 } }  ]

The second is to use the attribute Exclusions to exclude the x values for which the function is undefined.

Plot[ Piecewise[{ { x^2, -2≤ x< 1}, { - x +8, 4≤ x } }, Exclusions-> True ], {x, -2,6}, PlotRange->{ { -5, 7 }, { -1, 9 } }]

Try Your Own Examples or Modify the Givens Below

Prolog & Epilog: Adding Graphics Elements to Plots

Prolog and Epilog are similar, and in many simple presentions render the same appearance in output.  Consider the plot

ListPlot[{ {3,4} }]

Adding a red position vector can be achieved with both Prolog and Epilog.  The Mathematica input Grid[...] places the Plots side-by-side.  Take a moment to refer to the Documentation Center regarding the use of Grid[...] and Arrow[...].  The plot using Prolog is on the left.

Grid[{
{ ListPlot[{ { 3, 4 } }, Prolog->{ Red, Arrow[{ {0,0}, {3,4} }] }, ImageSize->Medium   ],
 ListPlot[{ { 3, 4 } }, Epilog->{ Red, Arrow[{ {0,0}, {3,4} }] }, ImageSize->Medium   ] }
}]

The main difference between Prolog and Epilog is the order in which graphics are drawn.  The graphics in Prolog are rendered first or lie underneath the main graphics.  The graphics in Epilog are rendered after the main graphics are drawn by Mathematica, or in effect, lie on top of the main graphics.  Consider,

Plot[ Sin[ x ], { x, 0, 2 Pi }, Prolog->{ Green, Thick,  Line[{ { Pi/2, 1 }, { 7 Pi / 4, - Sqrt[ 2 ] / 2 } }], Green,PointSize[0.015],Point[{{ Pi / 2,1},{ 7 Pi / 4,- Sqrt[ 2 ] / 2 }}] } ]

Since the Prolog graphics are rendered first the sine curve can be seen through the green line and green points.

Plot[ Sin[ x ], { x, 0, 2 Pi }, Epilog->{ Green,Thick,  Line[{ { Pi/2, 1 }, { 7 Pi / 4, - Sqrt[ 2 ] / 2 } }], Green,PointSize[0.015],Point[{{ Pi / 2,1},{ 7 Pi / 4,- Sqrt[ 2 ] / 2 }}] } ]

Since the Epilog graphics are rendered after the Plot[...] graphics the sine curve cannot be seen through the green line and green points.

Try Your Own Examples or Modify the Givens Below

Prolog & Epilog: A Few More Style and Label Elements

The example below demonstrates the use of Epilog to show a vector broken into its components.  Please note that a color specification will affect every element that follows in an Epilog or Prolog until a different color is specified.  Hence, “Black” applies to all the Text[...] elements.  If the “Black” specification is removed then all the Text[...] elements would be displayed in Orange.

ListPlot[{ {{ 1, 2}}, {{ 2, 4 }} }, PlotRange-> { { 0, 3 }, { 0, 5 }}, Epilog->{ Arrow[{ {1,2}, { 2, 4} }], Blue, Arrow[{ { 1, 2 }, { 2, 2 } }],Orange, Arrow[{ { 2, 2 }, { 2, 4 } }], Black, Text[ "vector", { 1.3, 3.2 } ], Text[ "x-component", {1.5,1.6}  ], Text[ "y-component", {2.2, 3}, {-0.5,0} ] }]

The following demonstrates how an Epilog can be separated into individual elements with {...}.  In this way colors and other style elements can be grouped.

Plot[Sin[x],{x,0,2 Pi},Epilog->{
{ Text["Extrema",{4,0.5},{-1,-1}]},
{Blue,Arrowheads[{-0.04,0.04}],Arrow[{{π/2,1},{4,0.5},{3π/2,-1}}]},{ PointSize[0.015],Point[{{ Pi / 2,1},{ 3Pi / 2,- 1 }}] } }, Ticks->{ {Pi / 2, Pi, 3 Pi / 2, 2 Pi}, { - 1, 1 }}]

Try Your Own Examples or Modify the Givens Below

Filling & FillingStyle: Shading Regions of a Plot[...]

Shading the region bounded by a curve and the x-axis is achieved using Filling.  Consider...

Plot[ x^3 - x, { x, -2, 2 }, Filling->Axis ]

FillingStyle can be used to change the default color.

Plot[ x^3 - x, { x, -2, 2 }, Filling->Axis, FillingStyle->Red ]

In order to change multiple characteristics of a FillingStyle a Directive[...] must be used.  In the case below, the default filling color is being changed to Red and the Opacity of the filling is being reduced.

Plot[ x^3 - x, { x, -2, 2 }, Filling->Axis, FillingStyle->Directive[ Red, Opacity[ 0.2 ]] ]

Try Your Own Examples or Modify the Givens Below

Filling: Shading Regions Bounded by Curves

There are several ways to shade the region in the first quadrant bounded by the curves y = x and y = (x^2).  Consider...

Plot[{ x, x^2 }, { x, 0, 1}, Filling->1->{2} ]

The previous example is probably the most straightforward way to produce the desired region; however, if more of the curves are to be seen then there exists the following option.

Show[
Plot[{ x, x^2 }, { x, - 0.5, 1.5 } ],
Plot[{ x, x^2 }, { x, 0, 1}, Filling->1->{2} ]
]

There are several ways to shade the region in the first and fourth quadrants bounded by the curves y = x and y = (x^3)-x.  The challenge here lies in finding the intersection.  Hopefully, it is obvious enough that x = 0 is one intersection and so only the first quadrant intersection needs to be computed.  This can be done by inserting a Solve[...] command into the Plot[...] like so.

Plot[{ x, x^3 - x}, { x, 0,Solve[ x == x^3 - x, x ][[3, 1, 2]]}, Filling->1{2} ]

Since

Solve[ x == x^3 - x, x ]

Syntactically, the solution list from the given Solve[...] is equivalent to

{ {{ x,0 }}, {{ x, -Sqrt[2] }}, {{ x, Sqrt[2] }} }

Use the Part[...] function or [[...]] to choose the square root of 2.  [[ 3, 1, 2 ]] tells Mathematica to choose the third list from the list of lists, then the first list inside the third list, and finally the second element of that list.

Solve[ x == x^3 - x, x ][[3, 1, 2]]

or

{ {{ x,0 }}, {{ x, -Sqrt[2] }}, {{ x, Sqrt[2] }} }[[ 3, 1, 2 ]]

or without using the shortcut notation...

Part[
Part[
Part[ { {{ x,0 }}, {{ x, -Sqrt[2] }}, {{ x, Sqrt[2] }} }, 3 ],
1],
2]

Similarly, if more of the curves are to be seen...

Show[
Plot[{ x, x^3 - x }, { x, - 0.5, 1.75 }],
Plot[{ x, x^3 - x}, { x, 0,Solve[ x == x^3 - x, x ][[3, 1, 2]]}, Filling->1{2} ]
]

Remeber that when trying to find roots that involve transcendental functions Mathematica requires the FindRoot[...] function.

Try Your Own Examples or Modify the Givens Below