Making Nicer Plots in Mathematica

We saw last time how to build a basic table:

LabelData={{"Chocolate (bars)", "Happiness (joys)"},{0,0},{1,2.7},{2,7.4},{3,20.1},{4,54.6},{5,148.4}}

Grid[LabelData,Alignment->Center,Spacings->{2,1}, Dividers->{False,{True,True,False}}]

and basic plots of data:

data={{0,1},{1,2.7},{2,7.4},{3,20.1},{4,54.6},{5,148.4}}

ListPlot[data]

Many scientists find that the default options for plots in Mathematica are not ideal for their purposes. The goal of this note is to show you how to change some of these options and to change their defaults so that you can consistently get the kind of plots you want.

In science one of the most important things you need to do for every plot—always ... without exception ... in every circumstance ... need I say it again?—is to label your axes. Mathematica has a built in options for this purpose, e.g. the AxesLabel command. Here is an example:

ListPlot[data,
AxesLabel-> {"Chocolate (bars)","Happiness (joys)"}]

When you give options you always specify them with a right arrow. You can type this directly by using first the dash key - and then the greater than symbol >. If you enter these two key strokes one after the other and then press the space key Mathematica automatically converts them into a right arrow. To tell Mathematica that something is text you use the double quotes. You will also notice that I have given AxesLabel two inputs in the form of a list. This option treats the 1st item of the list as the x-axis label and the 2nd as the y-axis label.

Here and below I will put each new option on a different line. This makes it easy to read the code and is a good habit for keeping track of what you are doing. That said, if you  are going to print out your whole notebook you probably don't want to do this. You can reach a new line by hitting return (or enter) without the shift key. As you know, the convention is to label the axis with the quantity measured and to put the units in parenthesis after the label.

For the purposes of being able to read where a data point is, it is often nice to have tick lines all the way around a plot. The Frame option accomplishes this:

ListPlot[data,
AxesLabel-> {"Chocolate (bars)","Happiness (joys)"},
Frame-> True]

You'll notice that we've lost our labels. This is because Mathematica thinks of Axes and Frames as two different things. When you use a Frame you want FrameLabel:

ListPlot[data,
Frame-> True,
FrameLabel-> {"Chocolate (bars)","Happiness (joys)"}]

Again it is often nice to have larger, easier to read labels. You can control the size and font of the labels with the LabelStyle command:

ListPlot[data,
Frame-> True,
FrameLabel-> {"Chocolate (bars)","Happiness (joys)"},
LabelStyle->{FontFamily-> "Arial",FontSize-> 16}]

Notice that FontFamily and FontSize are both options within the LabelStyle option. If you would like to resize your whole plot without having to always do it with the mouse you can use the ImageSize option:

ListPlot[data,
Frame-> True,
FrameLabel-> {"Chocolate (bars)","Happiness (joys)"},
LabelStyle->{FontFamily-> "Arial",FontSize-> 16},
ImageSize-> 600]

The ImageSize option expects a number that specifies the width of the plot in printer’s points by default (the same unit as you specify your font size with). One printer’s point is about 352.8 micrometers. A size that looks nice on the screen and prints well on the page is 600.

If you are plotting a function instead of a data set many of these options are the same. For example,

Plot[((E^x)-20),{x,-5,5},
Frame-> True,
FrameLabel-> {"x","y"},
LabelStyle->{FontFamily-> "Arial",FontSize-> 16},
ImageSize-> 600]

Here you see that Mathematica includes the axes through x=0 and y=0. If you want to suprress these you can use the Axes option:

Plot[((E^x)-20),{x,-5,5},
Axes-> False,
Frame-> True,
FrameLabel-> {"x","y"},
LabelStyle->{FontFamily-> "Arial",FontSize-> 16},
ImageSize-> 600]

At times you also want to make the line thicker or a different color, PlotStyle helps with this:

Plot[((E^x)-20),{x,-5,5},
Axes-> False,
Frame-> True,
FrameLabel-> {"x","y"},
LabelStyle->{FontFamily-> "Arial",FontSize-> 16},
ImageSize-> 600,
PlotStyle->{Black, Thickness[0.005]}]

As always, for all these options the Documentation is invaluable. You can also get a quick list of options, and their default values, using two question marks before the main command:

??ListPlot

In fact, you can reset the defaults and while this is not a good idea for many commands, for plotting it is perfect:

SetOptions[ListPlot,
Axes-> False, 
Frame-> True,
LabelStyle->{FontFamily-> "Arial",FontSize-> 16},
ImageSize-> 600];

I've used the semicolon at the end to surpress the output, which will just be the list of options with the new defaults specified. (You can restore the default settings using: SetOptions[Plot,PlotStyle->Automatic];)

Now we can call ListPlot without all of those options :

niceplot=ListPlot[data,
FrameLabel-> {"Chocolate (bars)","Happiness (joys)"}
]

Of course, we still need to specify our labels—here ... later ... and always.  A comment on combining plots using Show. First let

fitdata = Plot[(E^x), {x,0,5}]

If you combine your data and this fit using Show using the following,

Show[fitdata, niceplot]

notice that all of your nice sizing and labeling disappears. Basically, the Show command uses the options specified by the first graphics primitive you give to it. So ordering it as niceplot then fitdata gives a much better alternative:

moneyplot=Show[niceplot,fitdata]

Finally for including your plots in all the other things that you do (lab reports, papers, posters, etc.) it is nice to know how to export (and import) things from (to) Mathematica. The Export command does the trick: Export[“MoneyPlot.pdf”,moneyplot]. This first argument is the name you would like the file to have and the second is the Mathematica command you would like to supply the contents of the file. Unfortunately, if you execute this command directly it sends the file to Mathematica’s default directory, which you probably don’t know. A better option is to tell it explicitly where to send the file, like this:

Export["~/Desktop/MoneyPlot.pdf",moneyplot]

The squiggle in this expression (called a tilde) is your computer’s home directory. If you don’t know what directory this is, simply execute the command with “~/MoneyPlot.pdf” as the name and then search for the file on your computer. Once you’ve got a sense of where the home directory is you should be able to send the file anywhere you like.

Note that when you specify the file name with an extension Mathematica uses that extension (in our example .pdf) to determine the type of file to export. Other useful graphics formats are “.jpg”, “.gif”, and “.png”.

Importing works exactly the same way and once again you have to find out the exact name of the directory where the file is stored before you call the command. For example,

usethisplot= Import["~/Desktop/MoneyPlot.pdf"]

Almost everything about your plots is adjustable, although sorting through the documentation to figure it out can take some time. Once you have an option you like write a command to SetOptions so that  you can use it regularly. As you start to use Mathematica for more sophisticated graphics, like this

Table[ContourPlot[Re[f[x+I y]],{x,-Pi,Pi},{y,-Pi,Pi},PlotLabel->f,FrameTicks->{{-Pi,0,Pi},{-Pi,0,Pi}},ClippingStyle->Automatic,ColorFunction->"Pastel"],{f,{ArcSin,ArcCos,ArcCsc,ArcSec,ArcTan,ArcCot}}]

you will find that there are many pitfalls, especially when exporting. An excellent web resource that you can check out is: http://pages.uoregon.edu/noeckel/MathematicaGraphics.html