1
$\begingroup$

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: enter image description here

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?

$\endgroup$
2
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ Oct 8, 2015 at 21:08
  • 1
    $\begingroup$ You should know that upvoting good questions and answers is the way to tract attention to them. Since you're using the linked result I guess you could upvote it! $\endgroup$ Oct 8, 2015 at 21:10

1 Answer 1

2
$\begingroup$
att = Table[Dashing[{r, r}], {r, 0, 1, .03}]; 
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 -> Thread[{att, 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}}]]

TwoAxisPlot[{{x^2, x^3, x^4}, {1/x}}, {x, 0, 1}]

Mathematica graphics

$\endgroup$
2
  • 1
    $\begingroup$ But it was a trivial modification of your posted code. I believe you're using that code without understanding it :( $\endgroup$ Oct 8, 2015 at 21:30
  • $\begingroup$ I wasn't familiar with Thread thanks for you help $\endgroup$
    – dzeltzer
    Oct 8, 2015 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.