3
$\begingroup$

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

plot1

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]

plot2

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!

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

1 Answer 1

4
$\begingroup$

$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$

enter image description here

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}}]

enter image description here

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Vaggelis_Z
    Oct 18, 2013 at 7:29
  • $\begingroup$ 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. $\endgroup$
    – Vaggelis_Z
    Oct 18, 2013 at 7:47
  • $\begingroup$ If I understand correctly the "one line", then yes. $\endgroup$
    – Vaggelis_Z
    Oct 18, 2013 at 7:52
  • $\begingroup$ @Vaggelis_Z ok see my edit $\endgroup$
    – Kuba
    Oct 18, 2013 at 8:20
  • $\begingroup$ Yes, this is exactly what I wanted. Many thanks! $\endgroup$
    – Vaggelis_Z
    Oct 18, 2013 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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