MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: dual y-axis plotting - is it possible in mathematica?

  • To: mathgroup at
  • Subject: [mg98468] Re: dual y-axis plotting - is it possible in mathematica?
  • From: Bob F <deepyogurt at>
  • Date: Fri, 10 Apr 2009 04:55:50 -0400 (EDT)
  • References: <grkgr7$74l$>

On Apr 9, 3:57 am, David <anonym2.71... at> wrote:
> 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?


Did you try searching this group for previous examples? This is really
a great group and searching for a question usually provides a previous
asked question and several answers. I tried and came across several

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

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.


  • Prev by Date: Re: AMERICAN MATHEMATICAL MONTHLY -April 2009:Transformations Between
  • Next by Date: Re: Re: Re: UNDO and Mathematica - how useless
  • Previous by thread: Re: dual y-axis plotting - is it possible in mathematica?
  • Next by thread: Integrate and Piecewise