How to | 2つの縦軸を持つプロットを生成する方法
How to | 2つの縦軸を持つプロットを生成する方法
同じ定義域だが異なる範囲を持つ2つの関数があるとする.これらをPlotを使って一緒にプロットすると,
の値には同じ軸スケールが使われる.関数を比べるために,TwoAxisPlotは
の値を別々の軸スケール(左と右に)でプロットし,範囲が同じであるように見えるようにする.
以下では,2つの関数の間に簡単な関係があるのかどうかははっきりしない:
Plot[{Sin[x], 3Sin[x] + 5Cos[x]}, {x, 0, 4Pi}]ここで定義されるカスタム関数のTwoAxisPlotは,2つのプロットの大きさを変えて,同じ
の範囲があるかのように見せる.関数の範囲は枠の左と右に表示される:
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}]