3
$\begingroup$

Let's create some sample data

Clear["Global`*"];

data1 = {{1, 0.2}, {3, 1.2}, {5, -0.4}};
data2 = {{2, -0.9}, {4, -0.1}, {6, 0.3}};

L1 = ListPlot[data1, PlotStyle -> {Blue, PointSize[0.012]}];
L2 = ListPlot[data2, PlotStyle -> {Red, PointSize[0.012]}];

plot = Show[{L1, L2}, PlotRange -> All]

enter image description here

My question is the following: how can I connect with straight lines (in black color) all the data (from both lists), starting from that with the lowest value of $x$, up to the one with the largest value of $x$ coordinate?

Any suggestions?

$\endgroup$
6
$\begingroup$
data3  = SortBy[Join[data1, data2], First]; 

Show[ListPlot[data3, Joined -> True, PlotStyle -> Black], L1, L2]

or

ListPlot[data3, Joined -> True, PlotStyle -> Black,  Epilog -> {L1[[1]], L2[[1]]}]

or

ListPlot[{data1, data2, data3}, Joined -> {False, False, True},
  PlotStyle -> {Directive[Blue, PointSize[0.012]],
    Directive[Red, PointSize[0.012]], Directive[Black, PointSize[0]]}]

all give

enter image description here

Alternatively, using Mesh:

ListPlot[data3, Joined -> True, PlotStyle -> Black, BaseStyle -> PointSize[Large], 
  Mesh -> {Join[Thread[{data1[[All, 1]], Blue}], Thread[{data2[[All, 1]], Red}]]}]

enter image description here

$\endgroup$
  • $\begingroup$ II want the points to have the original color (red and blue); not all of them with the same color. $\endgroup$ – Vaggelis_Z Jan 2 '18 at 20:32
6
$\begingroup$
ListLinePlot[SortBy[Join[data1, data2], First], PlotStyle -> Black]

enter image description here

Update:

data = SortBy[Join[data1, data2], First];
ListLinePlot[data, PlotStyle -> Black, 
 Epilog -> {PointSize[Large], {Blue, Point[data1]}, {Red, Point[data2]}}]

enter image description here

$\endgroup$
  • $\begingroup$ Nice! But if I want also the points on the same diagram? $\endgroup$ – Vaggelis_Z Jan 2 '18 at 20:14
  • $\begingroup$ II want the points to have the original color (red and blue); not all of them with the same color. $\endgroup$ – Vaggelis_Z Jan 2 '18 at 20:27
1
$\begingroup$

You may do the following as well:

 ss = Riffle[data1, data2]

 ListLinePlot[ss,Mesh -> Full]
$\endgroup$
  • $\begingroup$ But if I want also the points on the same diagram? $\endgroup$ – Vaggelis_Z Jan 2 '18 at 20:22
  • $\begingroup$ Just add the option Mesh->Full $\endgroup$ – qahtah Jan 2 '18 at 20:25
  • $\begingroup$ II want the points to have the original color (red and blue); not all of them with the same color. $\endgroup$ – Vaggelis_Z Jan 2 '18 at 20:27

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.