1
$\begingroup$

This question already has an answer here:

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$

marked as duplicate by Kuba, MarcoB, Feyre, corey979, Jason B. plotting Feb 10 '17 at 17:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @Kuba: Thank this seems to help ... but I do now see any numbers at the right axis: imgur.com/a/fwypI $\endgroup$ – lio Feb 10 '17 at 14:24
  • $\begingroup$ @Kuba: Could you please show the code for my example? I am not able to make it running. $\endgroup$ – lio Feb 10 '17 at 14:33
  • $\begingroup$ @JasonB. it is at least documented. If your code fails one can't even complain. $\endgroup$ – Kuba Feb 10 '17 at 15:14
2
$\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$
  • $\begingroup$ Thank you for your help. Why is the right vertical axis (red) not positioned at x=1? $\endgroup$ – lio Feb 10 '17 at 14:58
  • $\begingroup$ @lio I am away from my computer. Maybe try ImagePadding as in previous answer $\endgroup$ – Jack LaVigne Feb 10 '17 at 15:29
  • $\begingroup$ Great ... yes this has to be added: for both plots ImagePadding -> 25,. Could you please update your answer? $\endgroup$ – lio Feb 10 '17 at 15:44
  • $\begingroup$ @lio Updated with ImagePadding $\endgroup$ – Jack LaVigne Feb 10 '17 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$ – Display Name Jul 17 at 12:15

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