2
$\begingroup$

I saw the answer here, but I don't see the right axes labels in Mathematica 11.3.

I have two data sets:

data1 = Get@"https://pastebin.com/raw/RDYqjCeA";
data2 = Get@"https://pastebin.com/raw/e3qcivH0";

I can plot the data points below each other for optical comparison:

minxdata1 = Min[data1[[All, 1]]];
maxxdata1 = Max[data1[[All, 1]]];
minydata1 = Min[data1[[All, 2]]];
maxydata1 = Max[data1[[All, 2]]];

minxdata2 = Min[data2[[All, 1]]];
maxxdata2 = Max[data2[[All, 1]]];
minydata2 = Min[data2[[All, 2]]];
maxydata2 = Max[data2[[All, 2]]];

plot1 = ListPlot[data1, 
   PlotRange -> {{minxdata1, maxxdata1}, {minydata1, maxydata1}}, 
   PlotStyle -> Blue, Frame -> True, 
   FrameLabel -> {{"y data1", ""}, {"x data1", ""}}, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
     FontFamily -> "Calibri"}, ImageSize -> Large];

plot2 = ListPlot[data2, 
   PlotRange -> {{minxdata2, maxxdata2}, {minydata2, maxydata2}}, 
   PlotStyle -> Red, Frame -> True, 
   FrameLabel -> {{"y data2", ""}, {"x data2", ""}}, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
     FontFamily -> "Calibri"}, ImageSize -> Large];

GraphicsColumn[{plot1, plot2}, ImageSize -> Large, 
 Spacings -> {{0, 0}, {0, 20}}]

enter image description here

I would like to plot both data sets in a single plot:

  • left and bottom axes for data1 (and labels x data1, y data1) should be in blue with corresponding axes numbers and data points also in blue

  • right and upper axes for data2 (and labels x data2, y data2) should be in red with corresponding axes numbers and data points also in red

How can this be solved?

$\endgroup$
  • $\begingroup$ I suggest to look at MinMax function. $\endgroup$ – OkkesDulgerci Jan 19 at 1:10
2
$\begingroup$

You can rescale data2 and put everything in a single ListPlot:

{{minmax1x, minmax1y}, {minmax2x, minmax2y}} = CoordinateBounds /@ {data1, data2};
data2transformed = RescalingTransform[{minmax2x, minmax2y}, {minmax1x, minmax1y}] @ data2;
xticks2 = Charting`FindTicks[minmax1x, minmax2x];
yticks2 = Charting`FindTicks[minmax1y, minmax2y];

ListPlot[{data1, data2transformed}, PlotStyle -> {Blue, Red},
 Frame -> True, FrameStyle -> {{Blue, Red}, {Blue, Red}}, 
 FrameTicks -> {{Automatic, yticks2 }, {Automatic, xticks2}},
 FrameLabel -> {{"y data1", "y data2"}, {"x data1", "x data2"}},
 BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, FontFamily -> "Calibri"},
 ImageSize -> Large, PlotRange -> {All, minmax1y}]

enter image description here

$\endgroup$
3
$\begingroup$

It might be easier to use the package SciDraw: (https://scidraw.nd.edu)

<< "SciDraw`"

p1 = ListPlot[data1, 
   PlotRange -> {{minxdata1, maxxdata1}, {minydata1, maxydata1}}, 
   PlotStyle -> Blue, Frame -> True, 
   FrameLabel -> {{"y data1", ""}, {"x data1", ""}}, 
   FrameTicks -> {{LinTicks[400, 800, 100, 4], 
      None}, {LinTicks[1500, 1600, 20, 4], None}}, 
   FrameTicksStyle -> Directive[Blue, 15], 
   FrameStyle -> {{Directive[Blue], Null}, {Directive[Blue], Null}}, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
     FontFamily -> "Calibri"}, ImageSize -> Large, ImagePadding -> 50];

p2 = ListPlot[data2, 
   PlotRange -> {{minxdata2, maxxdata2}, {minydata2, maxydata2}}, 
   PlotStyle -> Red, Frame -> True, 
   FrameLabel -> {{"", "y data2"}, {"", "x data2"}}, 
   FrameTicks -> {{None, LinTicks[400, 800, 100, 4]}, {None, 
      LinTicks[0, 80, 20, 4]}}, FrameTicksStyle -> Directive[Red, 15],
    FrameStyle -> {{Null, Directive[Red]}, {Null, Directive[Red]}}, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
     FontFamily -> "Calibri"}, ImageSize -> Large, ImagePadding -> 50];

Overlay[{p1, p2}]

enter image description here

EDIT

To skip SciDraw, (using the default ticks)

p1 = ListPlot[data1, 
   PlotRange -> {{minxdata1, maxxdata1}, {minydata1, maxydata1}}, 
   PlotStyle -> Blue, Frame -> True, 
   FrameLabel -> {{"y data1", ""}, {"x data1", ""}}, 
   FrameTicks -> {{All, None}, {All, None}}, 
   FrameTicksStyle -> Directive[Blue, 15], 
   FrameStyle -> {{Directive[Blue], Null}, {Directive[Blue], Null}}, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
     FontFamily -> "Calibri"}, ImageSize -> Large, ImagePadding -> 50];

p2 = ListPlot[data2, 
   PlotRange -> {{minxdata2, maxxdata2}, {minydata2, maxydata2}}, 
   PlotStyle -> Red, Frame -> True, 
   FrameLabel -> {{"", "y data2"}, {"", "x data2"}}, 
   FrameTicks -> {{None, All}, {None, All}}, 
   FrameTicksStyle -> Directive[Red, 15], 
   FrameStyle -> {{Null, Directive[Red]}, {Null, Directive[Red]}}, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
     FontFamily -> "Calibri"}, ImageSize -> Large, ImagePadding -> 50];

Overlay[{p1, p2}]
$\endgroup$
  • $\begingroup$ This is great. Is this not possible without SciDraw? I am using Mathematica 11.3. $\endgroup$ – mrz Jan 18 at 22:04
  • 1
    $\begingroup$ @mrz SciDraw runs in 11.3, you may ignore the warning messages; it's not exactly necessary, only LinTicks is used, which generates a table for the major and minor ticks, you can see how to generate such tables in the documentation, details section in reference.wolfram.com/language/ref/Ticks.html $\endgroup$ – egwene sedai Jan 18 at 22:17
  • 1
    $\begingroup$ @mrz you may also replace the LinTicks[...] by All and skip SciDraw to use the default tick choices, see my edits $\endgroup$ – egwene sedai Jan 18 at 22:20
2
$\begingroup$
plot1 = ListPlot[data1, PlotRange -> (MinMax@data1[[All, 2]] + 10), 
   PlotStyle -> Blue, Frame -> {{True, False}, {True, False}}, 
   FrameLabel -> {{"y data1", ""}, {"x data1", ""}}, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
     FontFamily -> "Calibri"}, ImageSize -> Large, 
   FrameStyle -> Directive[Blue], ImagePadding -> 50];

plot2 = ListPlot[data2, PlotRange -> (MinMax@data2[[All, 2]] + 10), 
   PlotStyle -> Red, Frame -> {{False, True}, {False, True}}, 
   FrameLabel -> {{"", "y data2"}, {"", "x data2"}}, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
     FontFamily -> "Calibri"}, ImageSize -> Large, 
   FrameStyle -> Directive[Red], ImagePadding -> 50, 
   FrameTicks -> All];

Overlay[{plot1, plot2}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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