Using Mathematica » Useful Features

Plotting

Mathematica has a host of different plot functions, but usually one only needs three of them:

Plot

Plot takes a function as its first argument and a variable and range as its second one. Example:

 Plot[Sin[x], {x, -π, π},
 ImageSize -> Small]
13-4513164947294324238

The function can also be defined outside of the Plot function itself:

 sinx = Sin[x];
Plot[sinx, {x, -π, π},
 ImageSize -> Small]
13-4776971268818325606

User defined functions are also valid, but they must first be called on the variable one wants to use.

That’s why the following fails:

 mySin[x_] := Sin[x];
Plot[mySin, {x, -π, π},
 ImageSize -> Small]
13-8826648132354525971

But the following will work:

 mySin[x_] := Sin[x];
Plot[mySin[x], {x, -π, π},
 ImageSize -> Small]
13-7279477836414445162

Plot can also plot many functions simultaneously, if the first argument is a list of appropriate functions:

 Plot[{Sin[x], Cos[x], Tan[x]}, {x, -π, π},
 ImageSize -> Small]
13-995204871575620383

These functions can be distinguished from each other via legends and styling. For example:

 Plot[{Sin[x], Cos[x], Tan[x]}, {x, -π, π},
 PlotLegends -> "Expressions",
 PlotStyle -> {
   Directive[Dashed, Gray],
   Directive[Thick, Purple],
   Directive[Thin, Red]
   },
 ImageSize -> Small
 ]
13-7135908203198493178

ListPlot

ListPlot is a variant of Plot that just takes a list of values and plots them. For example, let’s plot the first 1000 primes. The function we’re using here, Table , is explained more in the next section.

 ListPlot[Table[Prime[n], {n, 1000}],
 ImageSize -> Small]
13-3371394493544641033

Similarly to Plot , we can plot multiple data sets:

 ListPlot[{
  Table[Prime[n], {n, Range[2, 1000, 2]}],
  Table[-Prime[n], {n, Range[1, 999, 2]}]},
 ImageSize -> Small]
13-3688944527199105108

And again we can apply styling rules:


ListPlot[{
  Table[Prime[n], {n, Range[2, 1000, 2]}],
  Table[-Prime[n], {n, Range[1, 999, 2]}]},
 PlotLegends -> {"primen for \
n=2,4,6,...,1000",
   "-primen for n=1,3,5,...,999"},
 PlotStyle -> {Directive[AbsolutePointSize[5], Red], 
   Directive[AbsolutePointSize[.1], Purple]},
 ImageSize -> Small
 ]

13-5456555609594511692

We can also join the points to plot out a line


ListPlot[{
  Table[Prime[n], {n, Range[2, 1000, 2]}],
  Table[-Prime[n], {n, Range[1, 999, 2]}]},
 PlotLegends -> {"primen for \
n=2,4,6,...,1000",
   "-primen for n=1,3,5,...,999"},
 PlotStyle -> {Directive[Dashed, Red], Directive[Thick, Purple]},
 Joined -> True,
 ImageSize -> Small
 ]

13-5646297479297114505

Plot3D

Plot3D works just like Plot , but takes two ranges

 Plot3D[Sin[x + y], {x, -π, π}, {y, 0, 2 π},
 ImageSize -> Small]
13-2951848453403160234

Multiple functions:

 Plot3D[{Sin[x], Cos[y]}, {x, -π, π}, {y, 0, 2 π},
 ImageSize -> Small]
13-5260013713964025253

Styling:

 Plot3D[{Sin[x], Cos[y]}, {x, -π, π}, {y, 0, 2 π},
 PlotLegends -> "Expressions",
 MeshStyle -> None,
 PlotStyle -> {
   Directive[Specularity[Gray, 10], White],
   Directive[Purple, Specularity[Gray, 100], Opacity[.3]]},
 Lighting -> "Neutral",
 ImageSize -> Small]
13-5031790952252282029

Special Plots

Mathematica, being the massive system it is, has more-or-less every plot type one could desire built in. Most of these will not be generally useful, but it is worth keeping in mind that any plot type one could want, like, say, a 3D point-wise density-estimating plot, Mathematica has it. See: ListDensityPlot3D .

That specific plot will be useful later in finding local sinks from gradient functions and is not the kind of run-of-the-mill plot one sees everywhere.