OK, here comes my slightly changed version (GridLines
added, colors changed, function name changed):
twoAxisPlot[{f_, g_}, {x_, x1_, x2_},
gyRange : {gy1_, gy2_} : {Automatic, Automatic}] :=
Module[{fgraph, ggraph, frange, grange, fticks,
gticks}, {fgraph, ggraph} =
{Plot[f, {x, x1, x2}, Axes -> True, PlotStyle -> Blue,
PlotRange -> Automatic, GridLines -> Automatic],
Plot[g, {x, x1, x2}, Axes -> True, PlotStyle -> Red,
PlotRange -> gyRange]}; {frange,
grange} = (PlotRange /.
AbsoluteOptions[#, PlotRange])[[2]] & /@ {fgraph, ggraph};
fticks = N@FindDivisions[frange, 5];
gticks =
Quiet@Transpose@{fticks,
ToString[NumberForm[#, 2], StandardForm] & /@
Rescale[fticks, frange, grange]};
Show[fgraph,
ggraph /.
Graphics[graph_, s___] :>
Graphics[
GeometricTransformation[graph,
RescalingTransform[{{0, 1}, grange}, {{0, 1}, frange}]], s],
Axes -> False, Frame -> True,
FrameStyle -> {ColorData[1] /@ {1, 2}, {Automatic, Automatic}},
FrameTicks -> {{fticks, gticks}, {Automatic, Automatic}}]]
The function now has an optional argument gyRange
which defines the right y-axis, i.e. it can be envoced like so (as before):
twoAxisPlot[{Sin[x], 3 Sin[x] + 5 Cos[x]}, {x, 0, 4 Pi}]
or (e.g.):
twoAxisPlot[{Sin[x], 3 Sin[x] + 5 Cos[x]}, {x, 0, 4 Pi}, {-10, 15}]
Regards -- Henrik