Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.5K Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Create a two axis plot with fixed y range?

Posted 9 years ago

I have this code to plot with two y-axes, but the rage are automatically generated. I want the range to be fixed, so it always shows the y-axes with the same range independent of what I am plotting. Thank you in advance :)

The code can be found here as well:

http://reference.wolfram.com/language/howto/GeneratePlotsWithTwoVerticalScales.html

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], 3 Sin[x] + 5 Cos[x]}, {x, 0, 4 Pi}]
POSTED BY: Sara K
6 Replies
Posted 9 years ago

Thank you so much for helping me, now my plots looks way better.

Cheers, Sara

POSTED BY: Sara K

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

POSTED BY: Henrik Schachner
Posted 9 years ago

Hi Henrik,

This looks very useful. But I think there is a scaling problem. (In the first plot (no y range option), where does the 10^-6 come from?) Also, did you mean for the color of the axes tick labels to match color of the plots?

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}}]]

twoAxisPlot[{Sin[x], 3 Sin[x] + 5 Cos[x]}, {x, 0, 4 Pi}]

enter image description here

twoAxisPlot[{Sin[x], 3 Sin[x] + 5 Cos[x]}, {x, 0, 4 Pi}, {-10, 15}]

enter image description here

POSTED BY: David Keith

Hi David,

yes, you are absolutely right - this was not carefully done, thanks for spotting! Here comes a hopefully improved version:

twoAxisPlot[{f_, g_}, {x_, x1_, x2_}, 
  gyRange : {gy1_, gy2_} : {Automatic, Automatic}] := 
 Module[{fgraph, ggraph, frange, grange, fticks, gticks, plotColors},
  plotColors = {Blue, Red};
  {fgraph, ggraph} =
   {Plot[f, {x, x1, x2}, Axes -> True, PlotStyle -> plotColors[[1]], PlotRange -> Automatic, GridLines -> Automatic],
    Plot[g, {x, x1, x2}, Axes -> True, PlotStyle -> plotColors[[2]], PlotRange -> gyRange]}; {frange, grange} = (PlotRange /. AbsoluteOptions[#, PlotRange])[[2]] & /@ {fgraph, ggraph};
  fticks = N@FindDivisions[frange, 5];
  gticks = 
   Quiet[Transpose@{fticks, 
      ToString[NumberForm[#, 2], 
         StandardForm] & /@ (Threshold[#, 
           Abs[Subtract @@ MinMax[#]]/1000] &@
         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 -> {plotColors, {Automatic, Automatic}}, 
   FrameTicks -> {{fticks, gticks}, {Automatic, Automatic}}]]

And now we get:

enter image description here

Best regards -- Henrik

POSTED BY: Henrik Schachner
Posted 9 years ago

Thanks a lot! The right one (purple)

POSTED BY: Sara K

I would like to help, but for which one of the two y-axes do you want to keep the range fixed?

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard