# dual y-axis plotting - is it possible in mathematica?

3306 views

### David

Apr 9, 2009, 5:57:59 AM4/9/09
to
I'm trying to figure out how to create a dual y-axis scatterplot, but there doesn't seem to be any documentation about how to do this in Mathematica's help or anywhere online that comes up in a search engine quickly.

Anyone here know how to do it?

By dual y-axis plot, I mean a plot which has one y-axis on the left and one on the right, with different scales, but with a common x-axis. I need to be able to make such a plot because I would like to illustrate the measurement error of a plot of measured values, where the error is much smaller than the measured values. Because the data is this way, it's difficult to see variation in the error unless you have a second y-axis that's on a much smaller scale (say 0 to 0.1) vs. a scale of 0 to 50 for the measurement data.

There does seem to be plenty of documentation about how to do this in other systems, and I already know how to do it in Excel. I'm rather surprised there isn't an easily accessible way to do it in Mathematica... so, any ideas?

### David Park

Apr 10, 2009, 4:53:14 AM4/10/09
to
The Presentations package (\$50) among many other things has a CustomTicks
command that allows you to use ticks on any axis that have a 1-1 mapping to
the underlying plot values. You could then scale your error data, using
Rescale say, and provide matching tick values on one y-axis with normal tick
values on the other y-axis. With Presentations it is also easy to draw
multiple plot items along with primitives all in one plotting statement
without using Show or Epilog or having to switch graphics levels. You just
draw one thing after another.

The following page at my web site shows an example of a two y-axis graphic.

### Bob F

Apr 10, 2009, 4:55:39 AM4/10/09
to

David,

Did you try searching this group for previous examples? This is really
a great group and searching for a question usually provides a previous
suggestions:

1. One method involved using the "Frame" attribute and defining
specific tick marks, e.g.

Plot[{Sin[x], 3*Sin[x]}, {x, 0, 2*Pi},
PlotStyle -> {RGBColor[1, 0, 0], RGBColor[0, 0, 1]}, Frame -> True,
FrameTicks -> {Automatic, {{-3, "-3 M"}, {-2, "-2 M"}, {-1,
"-1 M"}, {1, "1 M"}, {2, "2 M"}, {3, "3 M"}},
Automatic, {{-3, "-30 T"}, {-20, "-2 T"}, {-1, "-10 T"}, {1,
"10 T"}, {2, "20 T"}, {3, "30 T"}}}]

2. Another method involved writing a user defined routine like:

TwoAxisPlot[{f_, g_}, {x_, x0_, x1_}, color1_, color2_,
opts : OptionsPattern[]] :=
Module[{f0, f1, g0, g1, gp, scale, gticks}, {f0, f1} =
Options[Plot[f, {x, x0, x1}, Frame -> True, opts], PlotRange][[1,
2, 2]];
{g0, g1} =
Options[gp = Plot[g, {x, x0, x1}, Frame -> True, opts],
PlotRange][[1, 2, 2]];
scale[y_] := f0 + ((f1 - f0) (y - g0))/(g1 - g0);
gticks =
Apply[{scale[#1], ##2} &,
AbsoluteOptions[gp, FrameTicks][[1, 2, 2]], {1}];
Plot[{f, scale[g]}, {x, x0, x1}, PlotRange -> 1.001 {f0, f1},
Frame -> True,
FrameTicks -> {{Automatic, gticks}, {Automatic, Automatic}},
PlotStyle -> {{color1}, {color2}},
FrameStyle -> {{color1, color2}, {{}, {}}}, opts]]

TwoAxisPlot[{Sin[x], 0.1 Cos[x]}, {x, -\[Pi], \[Pi]}, Red, Blue]

3. And a third suggested using David Parks "Presentations" package
which is available from him at

http://home.comcast.net/~djmpark/DrawGraphicsPage.html

One thing I thought was surprising was that I could not find a single
example of doing this on Wolfram's web site. The links that I came
across in Googling the topic all came up with links that have been
moved/deleted on Wolfram's web site and had no redirect to take me to
where they are now. So either they are gone from Wolfram's site or
they didn't bother to put the redirect in. Sad to see that Wolfram has
again chosen to remove useful content from their web site.

-Bob