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

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

You can plot several functions at once:

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

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

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

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

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

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

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

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

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

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

ListPlot[data]

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

ListPlot[data,PlotStyle->PointSize[0.05]]

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

ListPlot[data,PlotStyle->PointSize[0.05],PlotRange->{{-0.25,4.25},{0,20}}]

You can give specific numbers to PlotRange in the form {{left, right}, {bottom, top}}.  That works for most plot commands, not just ListPlot.  You can also do PlotRange->All and that will show almost everything in the plot.

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

Here you can see that ListPlot uses different styles for each list of points.  This is what you have to do to make them larger and different shapes:

ListPlot[{data,moreData},PlotMarkers->{Automatic,Large}]

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

xValues={1,2,3}

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

yValues={0,3,8}

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

{xValues,yValues}

You can see that with the MatrixForm command.

{xValues,yValues}//MatrixForm

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

points=Transpose[{xValues,yValues}]

ListPlot[points,PlotMarkers->{Automatic,Large},PlotRange->All]

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 ListPlot function can do this.  Plot can display any number of functions, and ListPlot 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:

data

pointPlot=ListPlot[data]

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

Now use Show to combine the two:

Show[functionPlot,pointPlot]

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

Show[functionPlot,pointPlot,PlotRange->All]

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:

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

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:

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

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:

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

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.

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

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

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

Saving plots for use in LATEX 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:

compositePlot=Show[functionPlot,pointPlot,PlotRange->All]

If you want to save this for LATEX, use either Encapsulated Post Script (or EPS) or Portable Document Format (or PDF).  These kinds of 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 or PDF files for all graphics in papers since they usually print their issues in at least 1200 dpi.

Export["CompositePlot.eps",compositePlot]

Then you can load it into a LATEX file with commands like this:

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\includegraphics{CompositePlot}

\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).

Export["CompositePlot.png",compositePlot]

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, PDF, or SVG files, Adobe Illustrator is great for editing them directly.  Inkscape, XFig, and Sketch and other free UNIX programs can sometimes edit them, or they might be able to import them in some non-editable way, but you can add stuff 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.