Re: two y-axes with different scaling

• To: mathgroup at smc.vnet.net
• Subject: [mg95906] Re: two y-axes with different scaling
• From: Bob F <deepyogurt at gmail.com>
• Date: Thu, 29 Jan 2009 05:58:22 -0500 (EST)
• References: <glpgf6\$ljf\$1@smc.vnet.net>

```On Jan 28, 4:44 am, Joerg <scha... at biologie.hu-berlin.de> wrote:
> Hi,
>
> I want to combine two 2-D plots into one.
> However, I want to have two different
> y-axes. One on the left and one two the right,
> with different ticklabels, respectively.
> How do I do that? I couln't find any
> example in the help files.
>
> Thanks,
>
> joerg

Found this on the www.advancedphysics.org web site from a jbiggs2: (at
http://www.advancedphysics.org/forum/showthread.php?t=9228 ) which
works when I tried it on my Mac version 7 of Mathematica:

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]]
TwoAxisListPlot[f_List, g_List, frange_, grange_, color1_, color2_,
opts___?OptionQ] :=
Module[{old, new, scale, fm, fM, gm, gM, newg}, {fm, fM} = frange;
{gm, gM} = grange;
scale[var_] := ((var - gm) (fM - fm))/(gM - gm) + fm;
old = AbsoluteOptions[
ListPlot[g, Frame -> True, PlotRange -> grange,
DisplayFunction -> Identity], FrameTicks][[1, 2, 2]];
new = (Prepend[Rest[#1], scale[First[#1]]] &) /@ old;
newg = Transpose[{Transpose[g][[1]],
Map[scale, Transpose[g][[2]], {1, 2}]}];
ListLinePlot[{f, newg}, Frame -> True,
FrameTicks -> {Automatic, Automatic, None, new},
PlotStyle -> {{color1}, {color2}},
FrameStyle -> {{}, {color1}, {}, {color2}},
PlotRange -> frange*(1 + .05 (fM - fm)), opts]]

for example, with

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

and

TwoAxisListPlot[Table[{x, Sin[x]}, {x, -\[Pi], \[Pi], .01}],
Table[{x, 0.1 Cos[x]}, {x, -\[Pi], \[Pi], .01}], {-1., 1}, {-0.2,
0.2}, Red, Blue]

-Bob

```

• Prev by Date: Re: Printing (v7)
• Next by Date: Re: Using Map or Thread over selected parts of nested lists
• Previous by thread: Re: two y-axes with different scaling
• Next by thread: Derivative