Adding a scale label to an axis in Mathematica

This is a trick that eluded me for a few days but finally I managed to RTFM it.

There’s no built-in option for this but it is easy to do once you know how.

Normally you would do this with AxesLabel->{"xlabel","ylabel"}.

But what do you do when you use a frame (Frame->True)? Which is what you would do when you prepare a good looking figure…

FrameLabel->{"xlabel","ylabel","x2label","y2label"} appear in the middle of the axes, as they should as they tell you what the axes are about.

But if you also want an axis scale, like 10^4 or 10^-5 then you need to use the following (for a y-axis label):

Epilog->Inset["10^3", ImageScaled[{0.03,0.97}]],

The important commands are in red.

FrameTicks allows you to choose which ticks are labeled but also HOW they are labeled – 2 instead of 2000, etc.

Epilog allows you to add some graphics to the plot.

Inset is… well here it allows you to add some text.

ImageScaled is a clever way to position the text. Usually you just give a couple of coordinates based on the plot axes (like 0,0 for something that will appear on the origin). But with ImageScaled you can position something on the whole image, and not just inside the plot area.

PlotRangeClipping is an important option to use, because it allows things outside of the plot area to be displayed instead of clipped.

Here’s the result:

The figure created by the code above

Fig. 1 – The exponential function

All the options and functions used (marked as code) can be easily RTFMed in Mathematica’s online documentation.

I’ve used Mathematica 8 so I’m not sure how these things respond to older versions.

See Also: