1
$\begingroup$

I am trying to plot curves with the same x-range but different y-axis scales.

Here's my attempt:

X = Table[i, {i, 0, 2, 0.01}];

plot1 = ListLinePlot[Transpose[{X, X - 1}], PlotStyle -> Red , ImagePadding -> 25, AxesOrigin -> {0, 0}];

plot2 = ListLinePlot[Transpose[{X, Sqrt[X]}],  Frame -> {False, False, False, True},  FrameTicks -> {{None, All}, {None, None}}, AxesOrigin -> {0, 0}, 
   ImagePadding -> 25];

Overlay[{plot1, plot2}]

The output:

enter image description here

As seen in the figure above, the two origins are not aligned because the blue curve is not above the x-axis, i.e., not starting at (0,0), and the red curve is not starting at (0, -1) when the axes are combined.

I did consult the answers here. However, they don't seem to work in Mathematica 10. I've also tried to use the resource function MultipleAxesListPlot, but I don't think it's supported in Mathematica 10. See the documentation of the resource function here.

Is there another method I can use to align the axes?

Your help will be much appreciated. Thank you.

$\endgroup$

1 Answer 1

4
$\begingroup$

There are several solutions.

The simplest one is to give the same plot range to the two plots:

X = Table[i, {i, 0, 2, 0.01}];
plot1 = ListLinePlot[Transpose[{X, X - 1}], PlotStyle -> Red, 
   ImagePadding -> 25, AxesOrigin -> {0, 0}, PlotRange -> {-1, 1.5}];
plot2 = ListLinePlot[Transpose[{X, Sqrt[X]}], 
   Frame -> {False, False, False, True}, 
   FrameTicks -> {{None, All}, {None, None}}, AxesOrigin -> {0, 0}, 
   ImagePadding -> 25, PlotRange -> {-1, 1.5}];
Overlay[{plot1, plot2}]

enter image description here

Many more solutions you may find in: TwoAxesPlots

**Addendum**

A simple hack for different axes that works for all versions of MMA would be to fiddle manually with the plot range like:

X = Table[i, {i, 0, 2, 0.01}];
plot1 = ListLinePlot[Transpose[{X, X - 1}], PlotStyle -> Red, 
   ImagePadding -> 25, AxesOrigin -> {0, 0}, PlotRange -> {-1, 1.5}];
plot2 = ListLinePlot[Transpose[{X, Exp[4 X]}], 
   Frame -> {False, False, False, True}, 
   FrameTicks -> {{None, All}, {None, None}}, AxesOrigin -> {0, 0}, 
   ImagePadding -> 25, PlotRange -> {-655, 1000}, 
   AxesOrigin -> {0, 0}];
Overlay[{plot1, plot2}]

enter image description here

$\endgroup$
3
  • $\begingroup$ If they have the same PlotRange there is no need for two axes. ListLinePlot[Evaluate@{Transpose[{X, X - 1}], Transpose[{X, Sqrt[X]}]}, PlotLegends -> Placed[{HoldForm[X - 1], HoldForm[Sqrt[x]]}, {.8, .2}]] $\endgroup$
    – Bob Hanlon
    Sep 5, 2022 at 14:30
  • $\begingroup$ Thank you for your answer @DanielHuber, but I was asking for the case where they have a completely different 'PlotRange', for example, if I were to plot a straight line and an exponential curve on the same axis, then I would need a secondary y-axis. I have consulted the answers in 'TwoAxesPlots' but the solutions support versions from 11.3 onwards. $\endgroup$
    – Jacob
    Sep 6, 2022 at 9:44
  • 1
    $\begingroup$ I added a crude manual hack that should work in any version of Mathematica $\endgroup$ Sep 6, 2022 at 10:30

Your Answer

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

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