# How to combine two plots with the same y-axis

I have a data file which contains three columns of data. The first and the second column correspond to $x$ coordinates, while the third column to $y$ coordinate. Here is a small sample of the data file:

data = {{0.5, -77.06771909999159, 0.0012271846271586164},
{1.0, 0.9928749927334053, 0.019634954034537862},
{1.5, 78.49520253892854, 0.09940195479984795},
{2.0, 173.19305308215831, 0.3141592645526058},
{2.5, 289.4742561958002, 0.7669903919741358},
{3.0, 428.890904029252, 1.5904312767975672},
{3.5, 592.1255900024181, 2.9464702898078396}}


For the first plot we use

S01 = ListPlot[Flatten[List /@ data[[All, {1, 3}]], 1],
Joined -> False, PlotStyle -> {PointSize[0.01], Black},
Axes -> False, Frame -> True, FrameLabel -> {"x1", "M"},
RotateLabel -> False, ImageSize -> 550]


which gives

and for the second plot

S02 = ListPlot[Flatten[List /@ data[[All, {2, 3}]], 1],
Joined -> False, PlotStyle -> {PointSize[0.01], Black},
Axes -> False, Frame -> True, FrameLabel -> {"x2", "M"},
RotateLabel -> False, ImageSize -> 550]


Now, I want to combine these two plots keeping the same $y$ axis as it is, set the first $x$ axis (and of course the corresponding labels and tick marks) at the bottom of the frame and the second $x$ axis at the top of the frame. Is this doable? If so, any suggestions?

Many thanks in advance!

• Why using Flatten[List /@ data[[All, {1, 3}]], 1] rather than just data[[All, {1, 3}]] ? Oct 18, 2013 at 7:32
• @b.gatessucks Just to make sure that there are no extra {}. Oct 18, 2013 at 7:36

$x1$ and $x2$ are not linearly correlated so we have to find a transformation, I decided to use 3rd degree polynomial:

 sol[x_] = Normal @ NonlinearModelFit[data[[;; , {1, 2}]],
a x^3 + b x^2 + c x + d, {a, b, c, d}, x]

Plot[sol[x], {x, 0, 4}, Epilog -> Point@data[[;; , {1, 2}]], AxesLabel -> {"x1", "x2"},
BaseStyle -> {15, [email protected]}]


$6.73694 x^3-3.34503 x^2+140.344 x-145.979$

Now we have to create ticks:

ticks = {x /. Solve[sol[x] == #, x, Reals][[1]],
#} & /@ FindDivisions[{sol@0, [email protected]}, 40];

ticks = MapIndexed[ If[Divisible[First@#2 - 1, 5], {##, {0.03, 0}} & @@ #,
{First@#, ""}] &
, ticks, 1];

ListPlot[data[[;; , {1, 3}]], Frame -> True, BaseStyle -> [email protected],
FrameTicks -> {{Automatic, Automatic}, {All, ticks}}]


• Not exactly like this. The x2 axis should be calibrated so as the points to match. Now apart the first and the last point, all the others appear double. Oct 18, 2013 at 7:29
• Yes, I know that in the second plot x steps are not equal. However, there must be a way to regulate the step in such a way in order to fit with the x step of the first plot thus producing one dot only not two. Oct 18, 2013 at 7:47
• If I understand correctly the "one line", then yes. Oct 18, 2013 at 7:52
• @Vaggelis_Z ok see my edit
– Kuba
Oct 18, 2013 at 8:20
• Yes, this is exactly what I wanted. Many thanks! Oct 18, 2013 at 8:27