Graphics and Plotting

Mathematica has a large number of built-in graphics programs for plotting and exploring mathematics graphically and for plotting and visualizing data.  Graphs and plots are effective ways of conveying complex information.  It is important to learn how to create and manipulate plots and graphics.  Mathematica is very rich in its graphical functions—we will explore just a small subset of all its capabilities.  We can get an idea of how many plotting routines are available byusing a wildcard:

In[116]:=

? *Plot*

System`
ArrayPlot ListContourPlot ListPlot3D ParametricPlot3D Plot3Matrix PlotLabel PlotRegion
ContourPlot ListDensityPlot MaxPlotPoints Plot PlotDivision PlotPoints PlotStyle
DensityPlot ListPlot ParametricPlot Plot3D PlotJoined PlotRange
Global`
GammaPlot
Graphics`Animation`
MovieContourPlot MovieDensityPlot MovieParametricPlot MoviePlot MoviePlot3D
Graphics`ContourPlot3D`
ContourPlot3D ListContourPlot3D
Graphics`FilledPlot`
FilledListPlot FilledPlot ListFilledPlot
Graphics`Graphics`
ErrorListPlot LinearLogListPlot ListAndCurvePlot LogLinearPlot LogLogListPlot LogPlot PolarPlot ScaledPlot
LabeledListPlot LinearLogPlot LogLinearListPlot LogListPlot LogLogPlot PolarListPlot ScaledListPlot TextListPlot
Graphics`Graphics3D`
ListShadowPlot3D ListSurfacePlot3D ScatterPlot3D ShadowPlot3D
Graphics`ImplicitPlot`
ImplicitPlot
Graphics`InequalityGraphics`
ComplexInequalityPlot InequalityPlot InequalityPlot3D
Graphics`Legend`
PlotLegend
Graphics`MultipleListPlot`
MultipleListPlot PlotSymbol
Graphics`ParametricPlot3D`
CylindricalPlot3D PointParametricPlot3D SphericalPlot3D
Graphics`PlotField`
ListPlotVectorField PlotGradientField PlotHamiltonianField PlotPolyaField PlotVectorField
Graphics`PlotField3D`
ListPlotVectorField3D PlotGradientField3D PlotVectorField3D

This is much less than are available in the standard packages; these will be discussed later, but here's what becomes available after loading the Graphics Packages:

In[117]:=

<<Graphics`

While Mathematica has a large number of plotting routines, no one program does everything we need.  It can be useful to export numbers and graphics from Mathematica and operate on them individually or with other specialized programs.  The number of different creative graphical solutions available grows geometrically with the number of different graphical tools that are mastered.
Here, we will just produce a number of examples of plots and graphics... (Note, I use a file "init.m" to set some options.  Your plots may look different from what we see in lecture. If you want to use my init.m you can download it from here. Where to locate your init.m file depends on the operating system you are using, check out init.m in the Master Index in the Help Browser.

Two-Dimensional Graphics

Plot, ParametricPlot, LogPlot, etc

In[119]:=

Plot[Sin[x]/x, {x, -5 Pi, 5Pi}]

[Graphics:HTMLFiles/Lecture-05_73.gif]

Out[119]=

-Graphics -

In[120]:=

Options[Plot]

Out[120]=

In[121]:=

Plot[Sin[x]/x, {x, -5Pi, 5Pi}, PlotRange→ {-0.25, 1.25}]

[Graphics:HTMLFiles/Lecture-05_78.gif]

Out[121]=

-Graphics -

In[122]:=

[Graphics:HTMLFiles/Lecture-05_81.gif]

Out[122]=

-Graphics -

In[123]:=

Plot[{Sin[x]/x, Tan[x]/x}, {x, -5Pi, 5Pi}, PlotRange→ {-0.25, 1.25}]

[Graphics:HTMLFiles/Lecture-05_84.gif]

Out[123]=

-Graphics -

In[124]:=

Plot[{Sin[x]/x, Tan[x]/x}, {x, -5Pi, 5Pi}, PlotRange→ {-0.25, 1.25}, PlotStyle→ {{Thickness[0.01], Hue[1]}, {Thickness[0.005], Hue[2/3]}}]

[Graphics:HTMLFiles/Lecture-05_87.gif]

Out[124]=

-Graphics -

ParametricPlot produces a 2-D graph as a function of a single parameter, e.g. t. The cartesian coordinates, e.g. x and y, are specified as functions x(t) and y(t). Thus, a continuous variation of the single parameter t will trace out a trajectory in the x-yˆplane.

In[125]:=

? ParametricPlot

In[126]:=

LuckyClover[t_  , n_] := (1/(n + 1)) {Cos[(n + 1) t - Pi/4] - (n + 1) Cos[t - Pi/4], Sin[(n + 1) t - Pi/4] - (n + 1) Sin[t - Pi/4]}

In[127]:=

ParametricPlot[LuckyClover[t, 4], {t, 0, 2 Pi}]

[Graphics:HTMLFiles/Lecture-05_93.gif]

Out[127]=

-Graphics -

In[128]:=

ParametricPlot[LuckyClover[t, 4], {t, 0, 2 Pi}, AspectRatio→1]

[Graphics:HTMLFiles/Lecture-05_96.gif]

Out[128]=

-Graphics -

The next statement attempts to use the Table function to produce a list of six LuckyClover functions and then make one parametric plot of all of them.

In[129]:=

ParametricPlot[Table[LuckyClover[t, i], {i, 2, 7}], {t, 0, 2 Pi}, AspectRatio→1]

ParametricPlot :: pptr : Table[LuckyClover[t, i], {i, 2, 7}] does not evaluate to a pair of real numbers at t = 2.61799*10^^-7.  More…

ParametricPlot :: pptr : Table[LuckyClover[t, i], {i, 2, 7}] does not evaluate to a pair of real numbers at t = 0.25489.  More…

ParametricPlot :: pptr : Table[LuckyClover[t, i], {i, 2, 7}] does not evaluate to a pair of real numbers at t = 0.532869.  More…

General :: stop : Further output of ParametricPlot :: pptr will be suppressed during this calculation. More…

[Graphics:HTMLFiles/Lecture-05_103.gif]

Out[129]=

-Graphics -

It is necessary to use the Evaluate command to get this to work.

In[130]:=

? Evaluate

Evaluate[expr] causes expr to be evaluated even if it appears as the argument of a function whose attributes specify that it should be held unevaluated. More…

In[131]:=

ParametricPlot[Evaluate[Table[LuckyClover[t, i], {i, 2, 7}]], {t, 0, 2 Pi}, AspectRatio→1]

[Graphics:HTMLFiles/Lecture-05_108.gif]

Out[131]=

-Graphics -

In[132]:=

ParametricPlot[Evaluate[Table[LuckyClover[t, i], {i, 2, 7}]], {t, 0, 2 Pi}, AspectRatio→1, PlotStyle→Table[{Thickness[0.005], Hue[(2/3) * (i - 2)/5]}, {i, 2, 7}]]

[Graphics:HTMLFiles/Lecture-05_111.gif]

Out[132]=

-Graphics -

In[133]:=

<<Miscellaneous`PhysicalConstants`

In[134]:=

BoltzmannConstant

ElectronCharge

SpeedOfLight

Out[134]=

(1.38065*10^-23 Joule)/Kelvin

Out[135]=

1.60218*10^-19 Coulomb

Out[136]=

(299792458 Meter)/Second

In[137]:=

Kelvin[TempCelcius_ ] := 273.15 + TempCelcius

In[138]:=

Arrhenius[EnergyEV_, TempCelcius_ ] := Exp[-(EnergyEV * Joule * ElectronCharge)/(Kelvin[TempCelcius] * BoltzmannConstant * Kelvin * Coulomb)]

In[139]:=

Arrhenius[e, t]

Out[139]=

^(-(11604.5 e)/(273.15 + t))

In[140]:=

ParametricPlot[{1/Kelvin[T], Log[Arrhenius[1., T]]}, {T, 0, 1000}]

[Graphics:HTMLFiles/Lecture-05_125.gif]

Out[140]=

-Graphics -

In[141]:=

[Graphics:HTMLFiles/Lecture-05_128.gif]

Out[141]=

-Graphics -

In[142]:=

Show[arrhenplot, Ticks→ {Table[{1/Kelvin[T], StringJoin["1/", ToString[T]]}, {T, -200, 500, 100}], Automatic}]

[Graphics:HTMLFiles/Lecture-05_131.gif]

Out[142]=

-Graphics -

In[143]:=

BankAccount[InitialInvestment_ , AnnualInterest_, NYears_ ] := InitialInvestment * (1 + AnnualInterest/100)^NYears

In[144]:=

Plot[BankAccount[100, 8.5, t], {t, 0, 50}]

[Graphics:HTMLFiles/Lecture-05_135.gif]

Out[144]=

-Graphics -

In[145]:=

<<Graphics`

In[146]:=

LogPlot[BankAccount[100, 8.5, t], {t, 0, 50}]

[Graphics:HTMLFiles/Lecture-05_139.gif]

Out[146]=

-Graphics -

Plotting Data

ListPlot, PieChart, Histogram, Barchart, etc

In[147]:=

<<Miscellaneous`ChemicalElements`

In[148]:=

Elements

Out[148]=

In[149]:=

e190 = Elements[[Table[i, {i, 1, 90}]]]

Out[149]=

In[150]:=

mps = Map[MeltingPoint[#] &, e190]/.Kelvin→1

MeltingPoint :: form : Melting point is for the diamond form of Carbon .

MeltingPoint :: form : Melting point is for the P4 form of Phosphorus .

MeltingPoint :: form : Melting point is for the alpha form of Sulfur .

General :: stop : Further output of MeltingPoint :: form will be suppressed during this calculation. More…

Out[150]=

The next plot illustrates the variation of melting temperature as a function of atomic number…

In[151]:=

ListPlot[mps]

[Graphics:HTMLFiles/Lecture-05_153.gif]

Out[151]=

-Graphics -

In[152]:=

ListPlot[mps, PlotJoined→True]

[Graphics:HTMLFiles/Lecture-05_156.gif]

Out[152]=

-Graphics -

In[153]:=

Dens = Map[Density[#] &, e190]/.{Kilogram→1, Meter→1}

Density :: temp : Density is for Hydrogen at 11 Kelvin.

Density :: tempform : Density is for the liquid form of Helium at 4 Kelvin.

Density :: form : Density is for the diamond form of Carbon .

Density :: temp : Density is for Nitrogen at 21 Kelvin.

Density :: temp : Density is for Oxygen at 55 Kelvin.

General :: stop : Further output of Density :: temp will be suppressed during this calculation. More…

Density :: tempform : Density is for the liquid form of Fluorine at 85 Kelvin.

Density :: form : Density is for the P4 form of Phosphorus .

Density :: form : Density is for the alpha form of Sulfur .

General :: stop : Further output of Density :: form will be suppressed during this calculation. More…

Density :: unknown : The density of Astatine is not known.

Density :: tempform : Density is for the liquid form of Radon at 211 Kelvin.

General :: stop : Further output of Density :: tempform will be suppressed during this calculation. More…

Density :: unknown : The density of Francium is not known.

Out[153]=

The next line matches up values of density with melting temperature…

In[154]:=

dmdata = Transpose[{Dens, mps}]

Out[154]=

In[155]:=

ListPlot[dmdata]

Graphics :: gptn : Coordinate Unknown in {Unknown, 575.} is not a floating-point number. More…

Graphics :: gptn : Coordinate Unknown in {Unknown, 300.} is not a floating-point number. More…

[Graphics:HTMLFiles/Lecture-05_179.gif]

Out[155]=

-Graphics -

In[156]:=

ListPlot[dmdata, PlotJoined→True]

Graphics :: gptn : Coordinate Unknown in {Unknown, 575.} is not a floating-point number. More…

Graphics :: gptn : Coordinate Unknown in {Unknown, 300.} is not a floating-point number. More…

[Graphics:HTMLFiles/Lecture-05_184.gif]

Out[156]=

-Graphics -

Three-Dimensional Graphics

Plot3D, ContourPlot, DensityPlot, etc

In[157]:=

EPot[x_, y_ , z_ , xo_ , yo_] := 1/((x - xo)^2 + (y - yo)^2 + z^2)^(1/2)

In[158]:=

SheetOLatticeCharge[x_, y_ , z_] := Sum[EPot[x, y, z, xo, yo], {xo, -5, 5}, {yo, -5, 5}]

SheetOLatticeCharge represents the electric field produced by an 11 by 11 array of point charges arranged on the x-y plane at z = 0. The following command evaluates and plots the field variation in the plane z = 0.25:

In[159]:=

Plot3D[Evaluate[SheetOLatticeCharge[x, y, 0.25]], {x, -6, 6}, {y, -6, 6}]

[Graphics:HTMLFiles/Lecture-05_189.gif]

Out[159]=

-SurfaceGraphics -

Note below how theplot is set to contain the output of the Plot3D command.

In[160]:=

theplot = Plot3D[Evaluate[SheetOLatticeCharge[x, y, 0.25]], {x, -6, 6}, {y, -6, 6}, PlotPoints→120]

[Graphics:HTMLFiles/Lecture-05_192.gif]

Out[160]=

-SurfaceGraphics -

Now we can adjust the viewpoint of theplot, without recalculating the entire plot, using the Show command:

In[161]:=

? Show

Show[graphics, options] displays two- and three-dimensional graphics, using the options specified. Show[g1, g2, ... ] shows several plots combined. More…

In[162]:=

? Show

Show[graphics, options] displays two- and three-dimensional graphics, using the options specified. Show[g1, g2, ... ] shows several plots combined. More…

In[163]:=

Show[theplot, ViewPoint→ {0, -5, 2}]

[Graphics:HTMLFiles/Lecture-05_199.gif]

Out[163]=

-SurfaceGraphics -

In[164]:=

theconplot = ContourPlot[Evaluate[SheetOLatticeCharge[x, y, 0.25]], {x, -6, 6}, {y, -6, 6}, PlotPoints→120]

[Graphics:HTMLFiles/Lecture-05_202.gif]

Out[164]=

-ContourGraphics -

In[165]:=

theconplot = ContourPlot[Evaluate[SheetOLatticeCharge[x, y, 0.25]], {x, -4, 4}, {y, -4, 4}, PlotPoints→120, ColorFunction→Hue, Contours→24]

[Graphics:HTMLFiles/Lecture-05_205.gif]

Out[165]=

-ContourGraphics -

In[166]:=

thedenplot = DensityPlot[Evaluate[SheetOLatticeCharge[x, y, 0.25]], {x, -4, 4}, {y, -4, 4}, PlotPoints→120, ColorFunction→ (Hue[1 - # * 0.66] &)]

[Graphics:HTMLFiles/Lecture-05_208.gif]

Out[166]=

-DensityGraphics -

In[167]:=

Show[thedenplot, Mesh→False]

[Graphics:HTMLFiles/Lecture-05_211.gif]

Out[167]=

-DensityGraphics -

Graphics Primitives

It can be useful to be able to build up arbitrary graphics objects piece-by-piece using simple "graphics primitives" like Circle:

In[168]:=

Show[Graphics[Circle[{2, 2}, 1.5]]]

[Graphics:HTMLFiles/Lecture-05_214.gif]

Out[168]=

-Graphics -

In[169]:=

Show[Graphics[Circle[{2, 2}, 1.5]], Axes->True]

[Graphics:HTMLFiles/Lecture-05_217.gif]

Out[169]=

-Graphics -

In[170]:=

Show[Graphics[Circle[{2, 2}, 1.5]], Axes->True, AxesOrigin→ {0, 0}, AspectRatio→1]

[Graphics:HTMLFiles/Lecture-05_220.gif]

Out[170]=

-Graphics -

Now we take a simple plot…

In[171]:=

cosplot = Plot[Cos[x], {x, 0, 4Pi}]

[Graphics:HTMLFiles/Lecture-05_223.gif]

Out[171]=

-Graphics -

and overlay some text in places of our own choosing…

In[172]:=

Show[cosplot, Graphics[Text["One Wavelength", {2 Pi, 1.1}]], Graphics[Text["Two Wavelengths", {4 Pi, 1.1}]], PlotRange→All]

[Graphics:HTMLFiles/Lecture-05_226.gif]

Out[172]=

-Graphics -

Many processes in the natural world can be modelled by random walks. An example is the trajectory of a particular  molecule in a gas at low pressure.  The next few lines of code produce random walks and display them graphically:

In[173]:=

randomwalk[0] = {0, {0, 0}}

Out[173]=

{0, {0, 0}}

In[174]:=

randomwalk[0][[2]]

Out[174]=

{0, 0}

In[175]:=

randomwalk[nstep_Integer ? Positive] := randomwalk[nstep] = {nstep, randomwalk[nstep - 1][[2]] + Random[Real, {0, 0.5}] * {Cos[theta = 2 Pi Random[]], Sin[theta]}}

In[176]:=

randomwalk[4]

Out[176]=

{4, {0.599323, 0.193247}}

In[177]:=

Show[Table[Graphics[Text[ToString[randomwalk[i][[1]]], randomwalk[i][[2]]]], {i, 0, 100}], PlotRange→All, AspectRatio→1, AxesOrigin→ {0, 0}]

[Graphics:HTMLFiles/Lecture-05_236.gif]

Out[177]=

-Graphics -

In[178]:=

[Graphics:HTMLFiles/Lecture-05_239.gif]

Out[178]=

-Graphics -

In[179]:=

<<Graphics`Animation`

In[180]:=

[Graphics:HTMLFiles/Lecture-05_293.gif]

In[181]:=

[Graphics:HTMLFiles/Lecture-05_345.gif]

In[182]:=

Out[182]=

-Graphics -

These last two plots have to do with the orientation dependence of the surface energy of a 2-D crystal (GammaPlot)and the equilibrium shape that a single crystal having this surface energy will adopt to minimize its total surface energy.

In[183]:=

gammaplot[theta_ , anisotropy_ , nfold_] := {Cos[theta] + anisotropy * Cos[(nfold + 1) * theta], Sin[theta] + anisotropy * Sin[(nfold + 1) * theta]}

In[184]:=

GammaPlot = ParametricPlot[gammaplot[t, 0.1, 4], {t, 0, 2 Pi}, AspectRatio→1, PlotStyle→ {{Thickness[0.005], RGBColor[1, 0, 0]}}]

[Graphics:HTMLFiles/Lecture-05_350.gif]

Out[184]=

-Graphics -

In[185]:=

Show[Table[wulffline[gammaplot[t, 0.1, 4], 2], {t, 0, 2 Pi, 2 Pi/100}], GammaPlot, AspectRatio→1]

[Graphics:HTMLFiles/Lecture-05_353.gif]

Out[185]=

-Graphics -

The inner envelope of the tangent lines to the GammaPlot in the figure above illustrates the energy-minimizing shape of the crystal. For this particular GammaPlot, the shape is approximately a square but the corners are rounded.

Export :: noopen : Cannot open /Volumes/Users_Partition_Primary/ccarter/c…6/Web/MathMATHML/Lecture-05/Lecture-05.xml.  More…

Export :: noopen : Cannot open /Volumes/Users_Partition_Primary/ccarter/c…6/Web/MathMATHML/Lecture-05/Lecture-05.xml.  More…

Export :: noopen : Cannot open /Volumes/Users_Partition_Primary/ccarter/c…6/Web/MathMATHML/Lecture-05/Lecture-05.xml.  More…


Created by Mathematica  (September 15, 2005) Valid XHTML 1.1!