3

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?

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

1 Answers1

6
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

Jack LaVigne
  • 14,462
  • 2
  • 25
  • 37
  • Thank you for your help. Why is the right vertical axis (red) not positioned at x=1? – lio Feb 10 '17 at 14:58
  • @lio I am away from my computer. Maybe try ImagePadding as in previous answer – Jack LaVigne Feb 10 '17 at 15:29
  • Great ... yes this has to be added: for both plots ImagePadding -> 25,. Could you please update your answer? – lio Feb 10 '17 at 15:44
  • @lio Updated with ImagePadding – Jack LaVigne Feb 10 '17 at 16:55
  • 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. – NeverMind Jul 17 '19 at 12:15