# Create a two axis plot with fixed y range?

**URL:** <https://community.wolfram.com/t/create-a-two-axis-plot-with-fixed-y-range/9751>\
**Category:** Questions\
**Tags:** graphics-and-visualization\
**Created:** [December 13, 2016, 1:09pm UTC](https://community.wolfram.com/t/create-a-two-axis-plot-with-fixed-y-range/9751 "2016-12-13T13:09:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![saraaasly](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@saraaasly](https://community.wolfram.com/u/saraaasly)\
**Post date:** [December 13, 2016, 1:09pm UTC](https://community.wolfram.com/t/create-a-two-axis-plot-with-fixed-y-range/9751/1 "2016-12-13T13:09:46Z")

</div>

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:

> **[Generate Plots with Two Vertical Scales—Wolfram Documentation](https://reference.wolfram.com/language/howto/GeneratePlotsWithTwoVerticalScales.html)**
>
> Suppose two functions have the same domain and different ranges. Plotting them together using Plot uses the same scale for the y values. To compare the functions, TwoAxisPlot plots them with different scales for the y values (on the left and right)...

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

```

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [December 14, 2016, 8:59pm UTC](https://community.wolfram.com/t/create-a-two-axis-plot-with-fixed-y-range/9751/2 "2016-12-14T20:59:18Z")

</div>

I would like to help, but for _which one_ of the _two_ y-axes do you want to keep the range fixed?

---

<div class="post-metadata">

**Author:** ![saraaasly](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@saraaasly](https://community.wolfram.com/u/saraaasly)\
**Post date:** [December 14, 2016, 9:14pm UTC](https://community.wolfram.com/t/create-a-two-axis-plot-with-fixed-y-range/9751/3 "2016-12-14T21:14:58Z")

</div>

Thanks a lot! The right one (purple)

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [December 15, 2016, 8:09am UTC](https://community.wolfram.com/t/create-a-two-axis-plot-with-fixed-y-range/9751/4 "2016-12-15T08:09:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saraaasly](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@saraaasly](https://community.wolfram.com/u/saraaasly)\
**Post date:** [December 15, 2016, 9:09am UTC](https://community.wolfram.com/t/create-a-two-axis-plot-with-fixed-y-range/9751/5 "2016-12-15T09:09:52Z")

</div>

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

Cheers,  
Sara

---

<div class="post-metadata">

**Author:** ![dlkeith](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dlkeith/32/31494_2.png) [@dlkeith](https://community.wolfram.com/u/dlkeith)\
**Post date:** [December 15, 2016, 5:51pm UTC](https://community.wolfram.com/t/create-a-two-axis-plot-with-fixed-y-range/9751/6 "2016-12-15T17:51:26Z")

</div>

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

```

 ![Twoaxisplot1.png](https://global.discourse-cdn.com/wolfram/original/2X/9/9ca89b46e986a0f80187c1740fcd6674c9e20b06.png)

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

```

 ![Twoaxisplot2.png](https://global.discourse-cdn.com/wolfram/original/2X/2/2b66c20d9d5d7914fb18d1970ba790b5daf0981c.png)

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [December 15, 2016, 7:30pm UTC](https://community.wolfram.com/t/create-a-two-axis-plot-with-fixed-y-range/9751/7 "2016-12-15T19:30:00Z")

</div>

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:

 ![ToAxisPlot.png](https://global.discourse-cdn.com/wolfram/original/3X/6/2/62bb6bc4ae563e0bde278e6e32c7ca882d13623e.png)

Best regards – Henrik
