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]:=

Out[1]=

You can plot several functions at once:

In[2]:=

Out[2]=

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

In[3]:=

Out[3]=

In[4]:=

Out[4]=

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]:=

Out[5]=

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]:=

Out[6]=

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

In[7]:=

Out[7]=

In[8]:=

Out[8]=

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

In[9]:=

Out[9]=

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

In[10]:=

Out[10]=

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]:=

Out[11]=

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]:=

In[13]:=

Out[13]=

In[14]:=

Out[14]=

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]:=

Out[15]=

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

In[16]:=

Out[16]=

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

In[17]:=

Out[17]=

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

In[18]:=

Out[18]=

You can see that with the MatrixForm command.

In[19]:=

Out[19]//MatrixForm=

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

In[20]:=

Out[20]=

In[21]:=

Out[21]=

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]:=

Out[22]=

In[23]:=

Out[23]=

In[24]:=

Out[24]=

Now use Show to combine the two:

In[25]:=

Out[25]=

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]:=

Out[26]=

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]:=

Out[27]=

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]:=

Out[28]=

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]:=

Out[29]=

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]:=

Out[30]=

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

In[31]:=

Out[31]=

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

Out[32]=

If you want to save this for , 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]:=

Out[33]=

Then you can load it into a 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]:=

Out[34]=

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)