Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

two y-axes with different scaling

2,214 views
Skip to first unread message

Joerg

unread,
Jan 28, 2009, 6:44:38 AM1/28/09
to
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

Jaccard Florian

unread,
Jan 29, 2009, 5:52:22 AM1/29/09
to
Hello Joerg,

If you look in the archives, you will see that this question has often come around.
There are several ways of doing it.

For example "by hand" (to plot y=sin(x) and y=3cos(x) with same looking amplitude):


d1 = Plot[{Sin[x], Cos[x]}, {x, 0, 10}, PlotRange -> {{0, 10}, {-1, 1}},

PlotStyle -> {Blue, Red}, Epilog -> {Text[Style["Sin(x)", FontColor -> Blue],

{2, 0.8}, Background -> White], Text[Style["3 Cos(x)", FontColor -> Red],

{5.2, 0.8}, Background -> White]}, DisplayFunction -> Identity];

Show[FullGraphics[d1] /. {{GrayLevel[0.], AbsoluteThickness[0.125],

Line[{{0., b_}, {c_, d_}}]} -> {Red, AbsoluteThickness[0.125],

Line[{{10, b}, {10 + c, d}}]}, Text[a_, {b_, c_}, {1., 0.}] ->

{Text[Style[3*a, FontColor -> Red], {b + 10, c}, {-1.5, 0}],

Text[Style[a, FontColor -> Blue], {b, c}, {1, 0}]}},

Graphics[{Red, Line[{{10, -1}, {10, 1}}]}], PlotRange -> {{-1.5, 12}, {-1.1, 1.1}}]

Or using what was on http://support.wolfram.com/mathematica/graphics/2d/twoaxisgraph.html (but I can't find it anymore) :

TwoAxisPlot[{f_, g_}, {x_, min_, max_}, frange_, grange_, (opts___)?OptionQ] :=

Module[{old, new, scale, pts, gstuff, fstuff}, {fm, fM} = frange;

{gm, gM} = grange; scale[var_] = ((var - gm)*(fM - fm))/(gM - gm) + fm;

old = AbsoluteOptions[Plot[g, {x, min, max}, Frame -> True, PlotRange -> grange,

DisplayFunction -> Identity], FrameTicks][[1,2,2]];

new = (Prepend[Rest[#1], scale[First[#1]]] & ) /@ old;

Plot[{f, scale[g]}, {x, min, max}, Frame -> True,

PlotRange -> frange + {0, 0.05*(fM - fm)}, FrameTicks ->

{Automatic, Automatic, None, new}, PlotStyle ->

{{Thickness[0.01], RGBColor[1, 0, 0]}, {Thickness[0.01], RGBColor[0, 1, 0]}},

FrameStyle -> {{}, {RGBColor[1, 0, 0]}, {}, {RGBColor[0, 1, 0]}}, opts]]

Example :

v[t_] := 3*t - 2

s[t_] := (3/2)*t^2 - 2*t

TwoAxisPlot[{v[t], s[t]}, {t, -10, 12}, {-50, 50}, {-100, 200},

GridLines -> {Range[-10, 12, 2], None},

Epilog -> {Text["v(t)", {-5, -20}, Background -> White],

Text["s(t)", {-8, 20}, Background -> White]}]

Regards

F.Jaccard

________________________________

David Park

unread,
Jan 29, 2009, 5:52:44 AM1/29/09
to
Joerg,

One method is to use Frame -> True, and FrameTicks -> tick specification,
and give your own specification for the ticks on the right hand side of the
plot. (Lookup Ticks to see how to make these specifications.) You can use
Automatic for the regular ticks and make a table for the right hand side.
However, sometimes this is a lot of work, especially if you want both the
major and minor ticks.

The curve that goes with the right hand side will also have to be scaled to
match the ticks.

The Presentations package from my web site has CustomTicks routines that
make it easy to produce any kind of tick scale that is a 1-1 mapping from
the underlying plot coordinates, with linear or log ticks. It also makes it
easy to combine curves from two or more plot types. (And many other
features.)

I think there is also a free TwoScalePlot package on the WRI MathSource that
may also be useful - but I don't know much about it.


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/

hayes...@gmail.com

unread,
Jan 29, 2009, 5:56:32 AM1/29/09
to
Hello Joerg:

The best thing I have found for customizing graphics is David Park's
Presentations package. It is hands down one of the best and most
practical third party applications.

Check out:

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

http://blackbook.mcs.st-and.ac.uk/~Peter/djmpark/html/

Hope that helps. Unfortunately the TwoAxisListPLot is no longer
available (from what I understand) and extending ListPlot to include
an arbitrary number of differently scaled functions isn't readily
transparent to me using the standard plots.

Cheers,

t.

dimitris

unread,
Jan 29, 2009, 5:56:54 AM1/29/09
to

Something like...

Plot[{Sin[x], Cos[x]}, {x, -Pi, Pi},
Frame -> {True, True, False, True}, Axes -> False,
FrameLabel -> {"x axis", "y1 axis", None, "y2 axis"},
FrameStyle -> {Automatic, Green, None, Orange},
FrameTicks -> {Range[-2*Pi, 2*Pi, Pi/2], Range[-1, 1, 0.5], None,
Range[-1, 1, 0.5]}]

???

Dimitris

Bob F

unread,
Jan 29, 2009, 5:58:10 AM1/29/09
to

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

dh

unread,
Jan 29, 2009, 6:00:21 AM1/29/09
to

Tugrul Temel

unread,
Jan 30, 2009, 2:31:09 PM1/30/09
to
Hi,

Do I have to buy the "Presentations" package or is it available for free? If
free, could you please send me web address?

Regards,
Tugrul

DrMajorBob

unread,
Jan 31, 2009, 6:45:21 AM1/31/09
to
It's $50, which is explained at the first link.

Bobby

--
DrMaj...@longhorns.com

0 new messages