To combine in 1 plot data with 2 scales, I use the following solution:
TwoAxisPlot[{f_, g_}, {x_, x1_, x2_}] :=
Module[{fgraph, ggraph, frange, grange, fticks,
gticks}, {fgraph, ggraph} =
MapIndexed[
Plot[#, {x, x1, x2}, BaseStyle -> {FontSize -> 18},
AxesLabel -> {"x", "y"},
PlotStyle -> {ColorData[1][#2[[1]]]}] &, {f, g}]; {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 -> True, Frame -> True,
FrameLabel -> {"XAxis", "YAxis 1", "", "YAxis 2"},
FrameStyle -> {ColorData[1] /@ {1, 2}, {Automatic, Automatic}},
FrameTicks -> {{fticks, gticks}, {Automatic, Automatic}}]]
E.g., TwoAxisPlot[{{x^2, x^3}, {1/x}}, {x, 0, 1}]
yields:
Yet I have trouble customizing line style within each scale. How, for example, can I make the curve of x^3
dashed, to distinguish it from x^2
?