2
$\begingroup$

I need to plot two ListPlots on two time-series with different scales, and draw both y axes separately. IE, something like this

enter image description here

This resource function provides this functionality for regular Plot, but it fails to reposition the second axes for ListPlot. Is there a better alternative?

combPlot = ResourceFunction["CombinePlots"];
xs = Range[100];
ys1 = xs;
ys2 = 10 xs + 10;
plot1 = ListPlot[Transpose[{xs, ys1}]]
plot2 = ListPlot[Transpose[{xs, ys2}], FrameStyle -> Red, 
  Frame -> True, PlotStyle -> Red]
combPlot[plot1, plot2]

enter image description here

$\endgroup$
5
  • $\begingroup$ Do the answers to this question address the issue. $\endgroup$
    – N.J.Evans
    Mar 6, 2023 at 18:34
  • $\begingroup$ The answer say to use CombinePlot resource function, which has the issue described $\endgroup$ Mar 6, 2023 at 18:35
  • 2
    $\begingroup$ Sorry - this question referenced in the comments has a good answer that doesn't use CombinePlots. $\endgroup$
    – N.J.Evans
    Mar 6, 2023 at 18:40
  • $\begingroup$ Aha, that indeed solve the issue. TLDR; combine two plots with Overlay, and move y-axis to the right using Frame -> {False, False, False, True}, FrameTicks -> {{False, All}, {False, False}} $\endgroup$ Mar 6, 2023 at 18:49
  • $\begingroup$ Your plots do not match the code. Were you using ListLogPlot for red set of points? $\endgroup$
    – Domen
    Mar 6, 2023 at 19:58

3 Answers 3

4
$\begingroup$

I'm surprised the easiest result (built-in functionality) was not yet mentioned:

xs = Range[100];
ys1 = xs^2;
ys2 = 10 xs + 10;
data1 = Transpose[{xs, ys1}];
data2 = Transpose[{xs, ys2}];
ListPlot[{data1, data2}, MultiaxisArrangement -> All]

enter image description here

$\endgroup$
1
  • $\begingroup$ good find, this seems like a new feature in 13 $\endgroup$ Mar 11, 2023 at 22:37
1
$\begingroup$

Alternatively,

Clear["Global`*"]

xs = Range[100];
ys1 = xs;
ys2 = 10 xs + 10;

ResourceFunction["MultipleAxesListPlot"][
 {Transpose[{xs, ys1}], Transpose[{xs, ys2}]},
 PlotStyle -> {Automatic, Red},
 "SecondaryAxesColor" -> Red,
 PlotLegends -> Placed[{"plot1", "plot2"},
   {.3, .7}]]

enter image description here

ResourceFunction["MultipleAxesListPlot"][
 {Transpose[{xs, ys1}], Transpose[{xs, ys2}]},
 Joined -> True,
 PlotStyle -> {Automatic, {Red, Dashed}},
 "SecondaryAxesColor" -> Red,
 PlotLegends -> Placed[{"plot1", "plot2"},
   {.3, .7}]]

enter image description here

$\endgroup$
1
$\begingroup$

ResourceFunction["CombinePlots"] does support ListPlot (in fact, it should support all 2D plot types, and any combination of them). You are simply missing some options:

combPlot = ResourceFunction["CombinePlots"];
xs = Range[100];
ys1 = xs;
ys2 = 10 xs + 10;
plot1 = ListPlot[Transpose[{xs, ys1}], Frame -> True]
plot2 = ListPlot[Transpose[{xs, ys2}], FrameStyle -> Red, 
  Frame -> True, PlotStyle -> Red]
combPlot[plot1, plot2, "AxesSides" -> "TwoY"]

enter image description here

First, note the added Frame->True option for the first plot: like Show, CombinePlots will use options from the first plot where applicable, including the Frame->False option. Second, note the "AxesSides" -> "TwoY" option needed to actually tell CombinePlots on which axes to put the individual plots.

$\endgroup$
1
  • $\begingroup$ You are right, that does that trick $\endgroup$ Mar 9, 2023 at 19:32

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.