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:

?*Plot*

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:

<< 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

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

Options[Plot]

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

\(Plot[Sin[x]/x, {x, \(-5\) Pi, 5  Pi}, 
      PlotRange -> {\(-0.25\), 1.25}, 
      AxesLabel -> {"x", \*""\!\(\(Sin \((x)\)\)\/x\)""}]\)

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

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

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.

?ParametricPlot

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]}

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

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

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.

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

?Evaluate

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

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}]]

<< Miscellaneous`PhysicalConstants`

BoltzmannConstant
ElectronCharge
SpeedOfLight

Kelvin[TempCelcius_ ]  :=  273.15  +  TempCelcius

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

Arrhenius[e, t]

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

arrhenplot  = 
      ParametricPlot[
        Evaluate[
          Table[{1/Kelvin[T], Log[Arrhenius[ev, T]]}, {ev, 1, 5, 
              1}]], {T, -200, 1000}, 
        PlotStyle -> 
          Table[{Thickness[0.005], Hue[(2/3)*(5 - i)/4]}, {i, 1, 5, 
              1}]]

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

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

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

<< Graphics`

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

Plotting Data

ListPlot, PieChart, Histogram, Barchart, etc

<< Miscellaneous`ChemicalElements`

Elements

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

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

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

ListPlot[mps]

ListPlot[mps, PlotJoined -> True]

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

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

dmdata  =  Transpose[{Dens, mps}]

ListPlot[dmdata]

ListPlot[dmdata, PlotJoined -> True]

Three-Dimensional Graphics

Plot3D, ContourPlot, DensityPlot, etc

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

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:

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

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

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

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

?Show

?Show

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

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

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

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

Show[thedenplot, Mesh -> False]

Graphics Primitives

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

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

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

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

Now we take a simple plot…

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

and overlay some text in places of our own choosing…

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

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:

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

randomwalk[0][[2]]

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

randomwalk[4]

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

Show[
Table[
        Graphics[
          Text[ToString[randomwalk[i][[1]]], randomwalk[
                i][[2]]]], {i, 0, 50}], 
Table[
        Graphics[
          Line[{randomwalk[j - 1][[2]], randomwalk[
                  j][[2]]}]], {j, 1, 
          50}], 
PlotRange -> All, 
      AspectRatio -> 1, AxesOrigin -> {0, 0}]

<< Graphics`Animation`

ShowAnimation[
Table[
Graphics[
{Text[
ToString[randomwalk[
                    i][[1]]], randomwalk[
                  i][[2]]],                     
Line[{randomwalk[i][[2]], randomwalk[
                    i + 1][[2]]}]}
], 
{i, 0, 
          49}
], 
PlotRange -> {{-3, 3}, {-3, 3}}, AspectRatio -> 1, 
      AxesOrigin -> {0, 0}
]

ShowAnimation[
Table[
Graphics[
Table[
{Text[
ToString[randomwalk[j][[1]]], randomwalk[
                    j][[2]]],                     
Line[{randomwalk[j][[2]], randomwalk[
                      j + 1][[2]]}]
}, 
{j, 0, 
              i}
]
], 
{i, 0, 
          49}
], 
PlotRange -> {{-3, 3}, {-3, 3}}, AspectRatio -> 1, 
      AxesOrigin -> {0, 0}
]

wulffline[{x_, y_}, wulfflength_]  =  
      Module[{theta, wulffhalflength = wulfflength*0.5, x1, x2, y1, y2}, 
        theta = ArcTan[x, y]; 
 
        x1  =  x  +  
            wulffhalflength*Cos[theta  +  Pi/2]; 
 
        x2 =   x  +  
            wulffhalflength*Cos[theta  -  Pi/2]; 
 
        y1  =  y  +  
            wulffhalflength*Sin[theta  +  Pi/2]; 
y2 =  
          y  +  wulffhalflength*
              Sin[theta  -  Pi/2]; 
Graphics[
          Line[{{x1, y1}, {x2, y2}}]]
]

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.

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

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

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

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.