Plotting How-to

by W. Garrett Mitchener

This worksheet shows a lot of different things you can do with plots, especially how to combine them in different ways.  It also shows a couple of techniques for working with lists of points in different forms.

Basic plots

Here's the basic function for plotting functions:   Plot

In[1]:=

Plot[x^2 - 1, {x, -3, 4}]

[Graphics:HTMLFiles/PlottingHowTo_2.gif]

Out[1]=

⁃Graphics⁃

You can plot several functions at once:

In[2]:=

Plot[{t^2 - 1, Sin[t]}, {t, -3, 4}]

[Graphics:HTMLFiles/PlottingHowTo_5.gif]

Out[2]=

⁃Graphics⁃

To make the curves different styles, use the option PlotStyle.

In[3]:=

Plot[{x^2 - 1, Sin[x]}, {x, -3, 4}, PlotStyle {{Hue[0.2]}, {Hue[0.8]}}]

[Graphics:HTMLFiles/PlottingHowTo_8.gif]

Out[3]=

⁃Graphics⁃

In[4]:=

Plot[{x^2 - 1, Sin[x]}, {x, -3, 4}, PlotStyle {{Thickness[0.001]}, {Thickness[0.01]}}]

[Graphics:HTMLFiles/PlottingHowTo_11.gif]

Out[4]=

⁃Graphics⁃

It's good to use Dashing or Thickness rather than Hue because you want the difference to show up clearly when you have to print on a black-and-white printer.

In[5]:=

Plot[{x^2 - 1, Sin[x]}, {x, -3, 4}, PlotStyle {{Dashing[{0.01, 0.01}]}, {Dashing[{0.03, 0.01}]}}]

[Graphics:HTMLFiles/PlottingHowTo_14.gif]

Out[5]=

⁃Graphics⁃

You can also combine the styles, which is even better because colors stand out on the screen while you're working or presenting:

In[6]:=

Plot[{x^2 - 1, Sin[x]}, {x, -3, 4}, PlotStyle {{Hue[0.2], Thickness[0.005], Dashing[{0.01, 0.01}]}, {Hue[0.8], Thickness[0.001], Dashing[{0.03, 0.01}]}}]

[Graphics:HTMLFiles/PlottingHowTo_17.gif]

Out[6]=

⁃Graphics⁃

If you have point data, you can display it with ListPlot.

In[7]:=

data = {{1, 1}, {2, 4}, {3, 9}, {4, 16}}

Out[7]=

{{1, 1}, {2, 4}, {3, 9}, {4, 16}}

In[8]:=

ListPlot[data]

[Graphics:HTMLFiles/PlottingHowTo_22.gif]

Out[8]=

⁃Graphics⁃

Those tiny dots are hard to see, so try this variation of the PlotStyle option:

In[9]:=

ListPlot[data, PlotStylePointSize[0.05]]

[Graphics:HTMLFiles/PlottingHowTo_25.gif]

Out[9]=

⁃Graphics⁃

Some of our dots got cut off.  You can fix that by specifying the PlotRange option.

In[10]:=

ListPlot[data, PlotStylePointSize[0.05], PlotRangeAll]

[Graphics:HTMLFiles/PlottingHowTo_28.gif]

Out[10]=

⁃Graphics⁃

You can also give specific numbers to PlotRange in the form {{left, right}, {bottom, top}}.  That works for most plot commands, not just ListPlot.

In[11]:=

ListPlot[data, PlotStylePointSize[0.05], PlotRange {{-1, 5}, {-1, 20}}]

[Graphics:HTMLFiles/PlottingHowTo_31.gif]

Out[11]=

⁃Graphics⁃

There's also a much more powerful function called MultipleListPlot that you have to load from a package.  The << command loads the Graphics package:

In[12]:=

<<Graphics`

In[13]:=

MultipleListPlot[{data}]

[Graphics:HTMLFiles/PlottingHowTo_35.gif]

Out[13]=

⁃Graphics⁃

In[14]:=

moreData = {{1, 3}, {2, 5}, {3, 0}, {4, 2}}

Out[14]=

{{1, 3}, {2, 5}, {3, 0}, {4, 2}}

Here you can see that MultipleListPlot uses different symbols for each list of points.  This is what you have to do to make them larger:

In[15]:=

MultipleListPlot[{data, moreData}, SymbolShape {PlotSymbol[Diamond, 6], PlotSymbol[Star, 6]}]

[Graphics:HTMLFiles/PlottingHowTo_40.gif]

Out[15]=

⁃Graphics⁃

How to turn a pair of lists into a list of pairs

In[16]:=

xValues = {1, 2, 3}

Out[16]=

{1, 2, 3}

(This should give you a spelling warning-- Don't worry about it, it's harmless.)

In[17]:=

yValues = {0, 3, 8}

General :: spell1 : Possible spelling error: new symbol name \"yValues\" is similar to existing symbol \"xValues\".  More…

Out[17]=

{0, 3, 8}

This expression creates a matrix whose two rows are xValues and yValues.

In[18]:=

{xValues, yValues}

Out[18]=

{{1, 2, 3}, {0, 3, 8}}

You can see that with the MatrixForm command.

In[19]:=

{xValues, yValues}//MatrixForm

Out[19]//MatrixForm=

( 1   2   3 )            0   3   8

We want two columns instead of two rows, so all we have to do is take the transpose.

In[20]:=

points = Transpose[{xValues, yValues}]

Out[20]=

{{1, 0}, {2, 3}, {3, 8}}

In[21]:=

ListPlot[points, PlotStylePointSize[0.05], PlotRange {{-1, 4}, {-1, 10}}]

[Graphics:HTMLFiles/PlottingHowTo_54.gif]

Out[21]=

⁃Graphics⁃

How to combine plots

Suppose you want to superimpose two plots, say one with a list of points and one with a function.  Neither the Plot function nor the MultipleListPlot function can do this.  Plot can display any number of functions, and MultipleListPlot can display any number of lists of points, but you can't mix the two directly.  Instead, you can save the actual graphics objects in a variable and use a special graphics function to show several of them at the same time.  Here's an example.

Let's continue with the data we defined earlier:

In[22]:=

data

Out[22]=

{{1, 1}, {2, 4}, {3, 9}, {4, 16}}

In[23]:=

pointPlot = ListPlot[data, PlotStylePointSize[0.025]]

[Graphics:HTMLFiles/PlottingHowTo_59.gif]

Out[23]=

⁃Graphics⁃

In[24]:=

functionPlot = Plot[x^2, {x, -3, 3}]

[Graphics:HTMLFiles/PlottingHowTo_62.gif]

Out[24]=

⁃Graphics⁃

Now use Show to combine the two:

In[25]:=

Show[functionPlot, pointPlot]

[Graphics:HTMLFiles/PlottingHowTo_65.gif]

Out[25]=

⁃Graphics⁃

One of our points got cut off.  That's because Show has to combine the options given to the different plots, so sometimes it doesn't get the range correct, and so forth.  To fix it, we just add a PlotRange option to Show

In[26]:=

Show[functionPlot, pointPlot, PlotRangeAll]

[Graphics:HTMLFiles/PlottingHowTo_68.gif]

Out[26]=

⁃Graphics⁃

The curve doesn't go all the way to that last point because we specified a range of {-3,3} when we created functionPlot.

3-Dimensional plotting

Mathematica can also plot surfaces.  Here's an example of the Plot3D command:

In[27]:=

Plot3D[2 - x^2 - y^2, {x, -2, 2}, {y, -2, 2}]

[Graphics:HTMLFiles/PlottingHowTo_71.gif]

Out[27]=

⁃SurfaceGraphics⁃

The surface is approximated by small flat faces.  There are zillions of options you can add to a surface plot.  One of the more useful ones is to control how fine the mesh is with the PlotPoints option:

In[28]:=

Plot3D[2 - x^2 - y^2, {x, -2, 2}, {y, -2, 2}, PlotPoints50]

[Graphics:HTMLFiles/PlottingHowTo_74.gif]

Out[28]=

⁃SurfaceGraphics⁃

If you use a fine mesh, the mesh lines can get distracting, so you might want to turn them off by specifying the Mesh option:

In[29]:=

Plot3D[2 - x^2 - y^2, {x, -2, 2}, {y, -2, 2}, PlotPoints50, MeshFalse]

[Graphics:HTMLFiles/PlottingHowTo_77.gif]

Out[29]=

⁃SurfaceGraphics⁃

Sometimes, a surface isn't the best way to view a function of two variables.  You can also use ContourPlot which draws and colors level sets of the function.

In[30]:=

ContourPlot[2 - x^2 - y^2, {x, -2, 2}, {y, -2, 2}]

[Graphics:HTMLFiles/PlottingHowTo_80.gif]

Out[30]=

⁃ContourGraphics⁃

And there's also DensityPlot, which splits the plane into boxes and colors each box based on the function value there.

In[31]:=

DensityPlot[2 - x^2 - y^2, {x, -2, 2}, {y, -2, 2}]

[Graphics:HTMLFiles/PlottingHowTo_83.gif]

Out[31]=

⁃DensityGraphics⁃

Saving plots for use in L A T E X and other programs

What if you want to save a picture to use in another program?  One way is to use the Export function.  It takes a file name in quotes and an object to export.  The format of the exported file is determined by the extension on the file name, for example, Picture.png is a PNG file, Picture.jpg is a JPEG file, etc.

Let's go back to the composite plot from before:

In[32]:=

compositePlot = Show[functionPlot, pointPlot, PlotRangeAll]

[Graphics:HTMLFiles/PlottingHowTo_87.gif]

Out[32]=

⁃Graphics⁃

If you want to save this for L A T E X, the best format is Encapsulated Post Script (or EPS for short).  EPS files are made up of drawing commands to be interpreted by a printer, and they can be drawn at any resolution.  Most journals want EPS files for all graphics in papers since they usually print their issues in at least 1200 dpi.

In[33]:=

Export["CompositePlot.eps", compositePlot]

Out[33]=

CompositePlot.eps

Then you can load it into a L A T E X file with commands like this:

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\includegraphics{CompositePlot.eps}

\end{document}

And the \includegraphics command takes options like

\includegraphics[width=1in]{...}
\includegraphics[height=1in]{...}

If you want to save this for the web or to put in a Word, Word Perfect, or Open Office document, a good format is Portable Network Graphics, or PNG.  This is a compressed, pixel-based graphics format, and Mathematica will render the graphics at a specific resolution.  Other formats that are good for the web are JPEG (which is more highly compressed because fine details are left out) and GIF (which is an older format similar to PNG).

In[34]:=

Export["CompositePlot.png", compositePlot]

Out[34]=

CompositePlot.png

If you want to edit the graphics after you've saved them, say to add arrows or other decorations, you have several options.  For EPS files, Adobe Illustrator is great for editing them directly.  XFig and Sketch and other free UNIX programs can import them, and then you can save the resulting figure as EPS.  For PNG and other pixel-based formats, try a paint program like Adobe Photoshop or the GIMP.  (Many paint programs can open EPS files, but they convert them to pixel data first, so you lose the ability to print them at any resolution.)   Word, Word Perfect, and Open Office can also do some drawing on top of imported graphics.

Export also has lots of options, but you probably won't need them unless you get into really fancy graphics.


Created by Mathematica  (January 21, 2005)