2
$\begingroup$

How can I align the lowest point and highest point of these two axes like 0 is aligned with 0 and 3 is aligned with 1.0 on the other axis?

The desired output is something like this (just to show the alignment and padding):

enter image description here

enter image description here

plot1 = Plot[x, {x, 0, 3}, PlotStyle -> {Red, Thick}, 
   LabelStyle -> Directive[Bold, 12, Black], ImagePadding -> True, 
   Frame -> {True, True, True, True}, 
   FrameStyle -> {Black, Red, Black, Transparent}, 
   GridLines -> {Range[0, 3, 0.2], Range[0, 3, 0.2]}];
plot2 = Plot[{x, 1/x}, {x, 0, 5}, 
   PlotStyle -> Directive[Blue, Thickness[0.01], CapForm["Round"]], 
   GridLines -> {Range[0, 3, 0.2], Range[0, 1, 0.2]}, 
   PlotRange -> {{0, 5}, {0, 1}}, ImagePadding -> True, 
   Frame -> {{True, False}, {True, True}}, 
   FrameStyle -> {{Directive[Blue, Thickness[0.005]], 
      ""}, {Directive[Black, Thickness[0.005]], ""}}];
ResourceFunction["CombinePlots"][plot1, plot2, "AxesSides" -> "TwoY"]
$\endgroup$
4
  • $\begingroup$ You could always play with PlotRange for plot2: PlotRange -> {{0, 5}, {-0.055, 1.055}}. This aligns the blue labels with the grid and has the advantage of showing the curves for x and 1/x crossing instead of appearing to be the same function. $\endgroup$ Sep 4, 2022 at 1:28
  • $\begingroup$ @Jean-Pierre how would you get the number 0.055? $\endgroup$
    – emnha
    Sep 4, 2022 at 5:51
  • $\begingroup$ This is just trial and error increasing the range to compress the axis and go around the problem while maintaining these empty areas around the curves. A more standard appearance (eliminating empty areas) would be to set the PlotRange of plot1: PlotRange -> {{0, 3}, {0, 3}}. $\endgroup$ Sep 4, 2022 at 12:17
  • $\begingroup$ @Jean-Pierre how can I keep the empy area (padding) like your first method without trial and error? It looks simple but doesn't seem so. $\endgroup$
    – emnha
    Sep 4, 2022 at 20:52

1 Answer 1

6
+100
$\begingroup$

Careful control of PlotRangePadding gets you what you want:

plot1 = Plot[x, {x, 0, 3}, PlotStyle -> {Red, Thick}, 
   LabelStyle -> Directive[Bold, 12, Black], ImagePadding -> True, 
   Frame -> {True, True, True, True}, 
   FrameStyle -> {Black, Red, Black, Transparent}, 
   GridLines -> {Range[0, 3, 0.2], Range[0, 3, 0.2]}, 
   PlotRangePadding -> Scaled[0.05]];
plot2 = Plot[{x, 1/x}, {x, 0, 5}, 
   PlotStyle -> Directive[Blue, Thickness[0.01], CapForm["Round"]], 
   GridLines -> {Range[0, 3, 0.2], Range[0, 1, 0.2]}, 
   PlotRange -> {{0, 5}, {0, 1}}, ImagePadding -> True, 
   Frame -> {{True, False}, {True, True}}, 
   PlotRangePadding -> Scaled[0.05], 
   FrameStyle -> {{Directive[Blue, Thickness[0.005]], 
      ""}, {Directive[Black, Thickness[0.005]], ""}}];

ResourceFunction["CombinePlots"][plot1, plot2, "AxesSides" -> "TwoY", 
 PlotRangePadding -> None]

enter image description here

Note how I set PlotRangePadding->Scaled[0.05] for both plots. Using a scaled value like this makes sure that the ticks line up in the final plot, since the same fraction is used up by the padding on both sides. I also added PlotRangePadding->None to the CombinePlots call: This gets rid of some extraneous padding that is otherwise added. This is caused by a bug in CombinePlots and should not be necessary (I have submitted a fixed version, but it might take a while until it is approved).

$\endgroup$

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.