5
$\begingroup$

I looked several similar questions but still have problems to write down the code by myself. I have two plots with the same x axis, is there a simple way to put them together but with different y scales? also how can I added two different y axis names?

Data1={{0., 0.}, {3.5, 2.6}, {7., 5.5}, {10.5, 6.2}, {14., 6.6}, {17.5, 7.8}}; Data2={{3.5, 2134.}, {7., 2250.}, {10.5, 2435.}, {14., 17.2}, {17.5, 16.8}}

$\endgroup$

2 Answers 2

8
$\begingroup$

You can use the new-in-version-13.0 option MultiaxisArrangement

llp = ListLinePlot[{Data1, Data2},  
    MultiaxisArrangement -> All, ImageSize -> Large] 

enter image description here

Unfortunately, due to a bug the option FrameLabel does not work as expected (see this Q/A). As a work-around we can use the function addFrameLabels from this answer to add frame labels:

addFrameLabels[][llp, 
    {{Style["data 1", 16], Style["data 2", 16]} , {Style["x", 16], None}}] 

enter image description here

$\endgroup$
2
  • $\begingroup$ thank you, what if I only want to show the points? I just need to change the "ListLinePlot" to "ListPlot"? $\endgroup$
    – zoey
    Feb 10, 2022 at 22:47
  • 1
    $\begingroup$ right. Or just add the option Joined -> False in ListLinePlot. $\endgroup$
    – kglr
    Feb 11, 2022 at 1:26
4
$\begingroup$

An alternative approach:

  1. Rescale the second column of Data2 so that it has the same range as the second column of Data1.
  2. Change the tick labels of the right frame using the function Charting`FindTicks

{yminmax1, yminmax2} = MinMax /@ {Data1[[All, 2]], Data2[[All, 2]]};

data2Rescaled = Transpose[{#, Rescale[#2, yminmax2, yminmax1]}] & @@ Transpose[Data2];

ListLinePlot[{Data1, data2Rescaled}, 
 Mesh -> All,
 ImageSize -> Large,
 Frame -> True,
 FrameStyle -> {Directive[LineColor -> ColorData[97]@#] & /@ {1, 2}, 
    {Automatic, Automatic}},
 FrameTicks -> {{Automatic, Charting`FindTicks[yminmax1, yminmax2]}, 
    {Automatic, All}},
 FrameLabel -> {{Style["data 1", 14], Style["data 2", 14]}, {Style["x", 14], None}}]

enter image description here

Use ListPlot instead of ListLinePlot to get

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you very much!!! $\endgroup$
    – zoey
    Feb 14, 2022 at 21:42

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.