2
$\begingroup$

Given

list={{5, 3}, {9, 5}, {10, 4}, {11, 3}, {13, 3}, {14, 4}}

I want to ListPlot these points and use Joined only for those points where the x-values differ by one. So here I would get two line segments {{9, 5}, {10, 4}, {11, 3}} and {{13, 3}, {14, 4}} . I tried to add conditions to Joined but could not get anywhere. I also want to Plot the single points left over. Any ideas?

$\endgroup$

3 Answers 3

3
$\begingroup$

You can also use RelationGraph as follows:

RelationGraph[#2[[1]] == #[[1]] + 1 &, list, VertexCoordinates -> list]

enter image description here

rg = RelationGraph[#2[[1]] == #[[1]] + 1 &, list, 
  VertexCoordinates -> list,
  EdgeStyle -> Directive[CapForm["Round"], Opacity[.5], AbsoluteThickness[15]],
  EdgeShapeFunction -> "Line"]

enter image description here

We can get the pieces using WeaklyConnectedComponents and style the edges and vertices using VertexShapeFunction and HighlightGraph:

wcc = WeaklyConnectedComponents[rg];

SeedRandom[12]
vshapes = Thread[Alternatives @@@ wcc -> 
    RandomSample[GraphElementData["VertexShapeFunction"], Length @ wcc]];

HighlightGraph[SetProperty[rg, {VertexSize -> .1, VertexShapeFunction -> vshapes}], 
 Subgraph[rg, #] & /@ wcc, GraphHighlightStyle -> "Thick"]

enter image description here

$\endgroup$
5
$\begingroup$
list = {{5, 3}, {9, 5}, {10, 4}, {11, 3}, {13, 3}, {14, 4}};

list2 = Split[list, #2[[1]] - #1[[1]] == 1 &];

ListPlot[list2, Joined -> True]

enter image description here

or with PlotMarkers

ListPlot[list2, Joined -> True, PlotMarkers -> Automatic]

enter image description here

$\endgroup$
3
  • $\begingroup$ Great! Thanks! But I forgot to mention that I want to plot the single points, too. $\endgroup$
    – user57467
    Commented Mar 5, 2021 at 13:38
  • 1
    $\begingroup$ @user57467 Please add that requirement to the question! $\endgroup$
    – MarcoB
    Commented Mar 5, 2021 at 13:42
  • $\begingroup$ "I want to ListPlot these points" seems clear enough to me. $\endgroup$
    – Brilliand
    Commented Mar 5, 2021 at 21:50
1
$\begingroup$

Or use FindClusters

FindClusters[{{5, 3}, {9, 5}, {10, 4}, {11, 3}, {13, 3}, {14, 4}}, 3, 
DistanceFunction -> (Abs[#1 - #2][[1]] &)]

{{{5, 3}}, {{9, 5}, {10, 4}, {11, 3}}, {{13, 3}, {14, 4}}}

$\endgroup$

Your Answer

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

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