3
$\begingroup$

I have two data sets data1 and data2:

SeedRandom[1];
data1 = Sort[RandomReal[1, {10, 2}]];
data2 = Sort[RandomReal[2, {10, 2}]];

I would like to plot both data sets in one common plot with two different colors.

  • the left vertical scale should extend from 0 to 1 for plot1
  • the right vertical scale should extend from 0 to 2 for plot2

How can I do that?

$\endgroup$
3
  • $\begingroup$ @Kuba: Thank this seems to help ... but I do now see any numbers at the right axis: imgur.com/a/fwypI $\endgroup$
    – lio
    Commented Feb 10, 2017 at 14:24
  • $\begingroup$ @Kuba: Could you please show the code for my example? I am not able to make it running. $\endgroup$
    – lio
    Commented Feb 10, 2017 at 14:33
  • $\begingroup$ @JasonB. it is at least documented. If your code fails one can't even complain. $\endgroup$
    – Kuba
    Commented Feb 10, 2017 at 15:14

1 Answer 1

6
$\begingroup$
SeedRandom[1];
data1 = Sort[RandomReal[1, {10, 2}]];
data2 = Sort[RandomReal[2, {10, 2}]];

Something about the grouping for Frame, FrameStyle and FrameTicks is a bit out of date in the previous answer. Try

plot1 = ListPlot[data1,
  PlotRange -> {{0, 1}, {0, 1}},
  PlotStyle -> Blue,
  ImagePadding -> 25,
  Frame -> {{True, False}, {True, True}}, 
  FrameStyle -> {{Blue, Automatic}, {Blue, Blue}}
  ]

Mathematica graphics

plot2 = ListPlot[data2,
  PlotRange -> {{0, 1}, {0, 2}},
  PlotStyle -> Red,
  ImagePadding -> 25,
  Axes -> False,
  Frame -> {{False, True}, {False, False}},
  FrameTicks -> {{None, All}, {None, None}}, 
  FrameStyle -> {{Automatic, Red}, {Automatic, Red}}
  ]

Mathematica graphics

Overlay[{plot1, plot2}]

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ Thank you for your help. Why is the right vertical axis (red) not positioned at x=1? $\endgroup$
    – lio
    Commented Feb 10, 2017 at 14:58
  • $\begingroup$ @lio I am away from my computer. Maybe try ImagePadding as in previous answer $\endgroup$ Commented Feb 10, 2017 at 15:29
  • $\begingroup$ Great ... yes this has to be added: for both plots ImagePadding -> 25,. Could you please update your answer? $\endgroup$
    – lio
    Commented Feb 10, 2017 at 15:44
  • $\begingroup$ @lio Updated with ImagePadding $\endgroup$ Commented Feb 10, 2017 at 16:55
  • $\begingroup$ Is it possible to align the y-axes? If I have one data set with just positive y values and another one with also negative y values then the two y-axes are misaligned. AxesOrigin doesn't seem to work here. $\endgroup$
    – NeverMind
    Commented Jul 17, 2019 at 12:15

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