How to| 生成具有两个垂直坐标的图形
假设两个函数的定义域相同而值域不同. 用 Plot 将两者绘在一起时,
值具有相同的尺度. 为了比较函数,可用 TwoAxisPlot 绘制,这时
值具有不同的尺度(分别在左和右),而两者的值域看起来是相同的.
Plot[{Sin[x], 3Sin[x] + 5Cos[x]}, {x, 0, 4Pi}]这里的自定义函数 TwoAxisPlot 将两幅图形的坐标重新调整,使
的范围大致相同. 函数的值域在边框的左边和右边显示:
TwoAxisPlot[{f_, g_}, {x_, x1_, x2_}] := Module[{fgraph, ggraph, frange, grange, fticks, gticks}, {fgraph, ggraph} = MapIndexed[Plot[#, {x, x1, x2}, Axes -> True, 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 -> False, Frame -> True, FrameStyle -> {ColorData[1] /@ {1, 2}, {Automatic, Automatic}}, FrameTicks -> {{fticks, gticks}, {Automatic, Automatic}}]]TwoAxisPlot[{Sin[x], 3Sin[x] + 5Cos[x]}, {x, 0, 4Pi}]