# Organize and identify data of points to data of lines

I have a data set of points in 3D, which consists of a few lines. data is given as a list of points for each z-plane. I plot these points, where eyes can easily identify 4 lines. (Note that near the top there is a U-shaped line; one might also regard it as a creation at some z-plane of 2 lines towards the top. It doesn't matter for my purpose as I only need to distinguish it from other lines.)

My question: How to identify those lines and plot them instead of the points? Also, I'd like to be able to specify colors for some of those lines.

data = {{{0, -0.3964566366623803, 0.02}, {0, -0.06430918702671802,
0.019999999999999997}, {0, 0.09741391627375345,
0.02}}, {{0, -0.3662523217602514,
0.039999999999999994}, {0, -0.05030718674096292, 0.04}, {0,
0.11431633090441498,
0.039999999999999994}}, {{0, -0.3587512501785969,
0.06}, {0, -0.029804257751107233, 0.06}, {0,
0.13501928846978148, 0.06}}, {{0, -0.3521503071867409,
0.07999999999999999}, {0, -0.00980140020002849,
0.07999999999999999}, {0, 0.15582226032290333,
0.07999999999999999}}, {{0, -0.3459494213459065,
0.09999999999999999}, {0, 0.00980140020002864,
0.09999999999999999}, {0, 0.17642520360051447,
0.09999999999999999}}, {{0, -0.34024860694384906,
0.11999999999999998}, {0, 0.029604229175596592,
0.11999999999999997}, {0, 0.19712816116588092,
0.11999999999999998}}, {{0, -0.3353479068438348,
0.13999999999999999}, {0, 0.04930704386340912,
0.13999999999999999}, {0, 0.21783111873124744,
0.13999999999999999}}, {{0, -0.33124732104586363,
0.15999999999999995}, {0, 0.06910987283897707,
0.15999999999999998}, {0, 0.23833404772110311,
0.15999999999999998}}, {{0, -0.3281468781254464,
0.17999999999999997}, {0, 0.08911273039005582,
0.17999999999999997}, {0, 0.25903700528646956,
0.17999999999999997}}, {{0, -0.32604657808258314,
0.19999999999999996}, {0, 0.10911558794113452,
0.19999999999999996}, {0, 0.27963994856408064,
0.19999999999999996}}, {{0, -0.3249464209172738,
0.21999999999999995}, {0, 0.12911844549221324,
0.21999999999999997}, {0, 0.3002428918416918,
0.21999999999999995}}, {{0, -0.32474639234176306,
0.23999999999999996}, {0, 0.14912130304329196,
0.23999999999999996}, {0, 0.32084583511930287,
0.23999999999999996}}, {{0, -0.3251464494927846,
0.25999999999999995}, {0, 0.16902414630661533,
0.25999999999999995}, {0, 0.34144877839691395,
0.25999999999999995}}, {{0, -0.32574653521931707,
0.27999999999999997}, {0, 0.18912701814544947,
0.27999999999999997}, {0, 0.36195170738676957,
0.27999999999999997}}, {{0, -0.3260465780825832,
0.29999999999999993}, {0, 0.20892984712101736, 0.3}, {0,
0.38255465066438066, 0.3}}, {{0, -0.3251464494927846,
0.31999999999999995}, {0, 0.22883269038434073,
0.31999999999999995}, {0, 0.4030575796542364,
0.31999999999999995}}, {{0, -0.3216459494213459,
0.33999999999999997}, {0, 0.24873553364766407,
0.33999999999999997}, {0, 0.4234604943563367,
0.33999999999999997}}, {{0, -0.31374482068866977, 0.36}, {0,
0.268538362623232, 0.36}, {0, 0.443863409058437,
0.36}}, {{0, -0.2998428346906701, 0.37999999999999995}, {0,
0.2883411915987999, 0.37999999999999995}, {0,
0.4640662951850265,
0.37999999999999995}}, {{0, -0.28154022003143303,
0.39999999999999997}, {0, 0.3080440062866125,
0.39999999999999997}, {0, 0.4841691670238606,
0.39999999999999997}}, {{0, -0.262737533933419,
0.41999999999999993}, {0, 0.327746820974425,
0.41999999999999993}, {0, 0.5041720245749395,
0.41999999999999993}}, {{0, -0.2456350907272467,
0.43999999999999995}, {0, 0.3474496356622376,
0.43999999999999995}, {0, 0.5238748392627519,
0.43999999999999995}}, {{0, -0.23063294756393762,
0.45999999999999996}, {0, 0.36715245035005006,
0.45999999999999996}, {0, 0.5433776253750537,
0.45999999999999996}}, {{0, -0.21693099014144873,
0.4799999999999999}, {0, 0.3867552507501073,
0.4799999999999999}, {0, 0.5626803829118446,
0.4799999999999999}}, {{0, -0.2044292041720245,
0.4999999999999999}, {0, 0.40655807972567515,
0.49999999999999994}, {0, 0.5815830832976141,
0.49999999999999994}}, {{0, -0.19282754679239886,
0.5199999999999999}, {0, 0.4262608944134878,
0.5199999999999999}, {0, 0.6001857408201172,
0.5199999999999999}}, {{0, -0.18162594656379477,
0.5399999999999999}, {0, 0.44616373767681106,
0.5399999999999999}, {0, 0.6181883126160881,
0.5399999999999999}}, {{0, -0.17112444634947843,
0.5599999999999999}, {0, 0.4661665952278897,
0.5599999999999999}, {0, 0.6359908558365481,
0.5599999999999999}}, {{0, -0.16092298899842827,
0.5799999999999998}, {0, 0.4862694670667239, 0.58}, {0,
0.6532933276182313, 0.58}}, {{0, -0.15112158879839968,
0.6}, {0, 0.5065723674810687, 0.6}, {0, 0.6699957136733821,
0.6}}, {{0, -0.14142020288612653, 0.6199999999999999}, {0,
0.5271753107586798, 0.6199999999999999}, {0,
0.6862980425775111,
0.6199999999999999}}, {{0, -0.1321188741248749,
0.6399999999999999}, {0, 0.5481783111873126,
0.6399999999999999}}, {{0, -0.12281754536362331,
0.6599999999999999}, {0, 0.5694813544792114,
0.6599999999999999}}, {{0, -0.11381625946563786,
0.6799999999999999}, {0, 0.591484497785398,
0.6799999999999999}}, {{0, -0.10491498785540783,
0.6999999999999998}, {0, 0.614087726818117,
0.7}}, {{0, -0.09611373053293323, 0.7199999999999999}, {0,
0.6373910558651237,
0.7199999999999999}}, {{0, -0.08721245892270318,
0.7399999999999999}, {0, 0.6617945420774397,
0.7399999999999999}}, {{0, -0.07851121588798393, 0.76}, {0,
0.6870981568795543,
0.7599999999999999}}, {{0, -0.06980997285326468,
0.7799999999999999}}, {{0, -0.061108729818545426,
0.7999999999999999}}, {{0, -0.052207458208315405,
0.8199999999999998}}, {{0, -0.5755822260322904,
0.84}, {0, -0.04320617231032997,
0.8399999999999999}}, {{0, -0.6294899271324474,
0.8599999999999998}, {0, -0.4558651235890841,
0.86}, {0, -0.03390484354907841,
0.8599999999999999}}, {{0, -0.6422917559651379,
0.8799999999999999}, {0, -0.3864552078868409,
0.8799999999999999}, {0, -0.02410344334904979,
0.8799999999999999}}, {{0, -0.6475925132161737,
0.8999999999999999}, {0, -0.3256465209315616,
0.8999999999999998}, {0, -0.013701957422488809,
0.8999999999999999}}, {{0, -0.6494927846835261,
0.9199999999999999}, {0, -0.2692384626375196,
0.9199999999999999}, {0, -0.0021003000428632124,
0.9199999999999999}}, {{0, -0.6492927561080154,
0.9399999999999997}, {0, -0.2154307758251178,
0.9399999999999998}, {0, 0.011501643091870295,
0.9399999999999998}}, {{0, -0.6474924989284183,
0.9599999999999999}, {0, -0.16482354622088863,
0.9599999999999999}, {0, 0.028104014859265725,
0.9599999999999999}}, {{0, -0.6429918559794255,
0.9799999999999999}, {0, -0.11901700242891833,
0.9799999999999999}, {0, 0.050107158165452303,
0.9799999999999999}}, {{0, -0.5782826118016858,
0.9999999999999999}, {0, -0.08091155879411338,
0.9999999999999999}, {0, 0.07901128732676105,
0.9999999999999999}}};

ListPointPlot3D[#, PlotRange -> {{-0.5, 0.5}, {-0.7, 0.7}, {0, 1}},
AxesLabel -> {"x", "y", "z"}, BoxRatios -> {1, 1, 2},
ImageSize -> Medium] &@Flatten[data, 1]


## Edit:

Another data2 set without U-shaped lines. 6 lines.

data2 = {{{0, -0.5874287428742875, -0.5}, {0, -0.41017101710171017, \
-0.5}, {0, 0.4103810381038103, -0.5}, {0,
0.5870787078707871, -0.5}}, {{0, -0.5592159215921593, \
-0.47959183673469385}, {0, -0.37635763576357634, \
-0.47959183673469385}, {0,
0.4385238523852385, -0.47959183673469385}, {0,
0.6206820682068206, -0.47959183673469385}}, {{0, \
-0.5368136813681368, -0.45918367346938777}, {0, \
-0.3392539253925392, -0.45918367346938777}, {0,
0.4609260926092609, -0.45918367346938777}, {0,
0.6576457645764576, -0.45918367346938777}}, {{0, \
-0.5188918891889189, -0.4387755102040816}, {0, \
-0.3018701870187019, -0.4387755102040816}, {0,
0.47884788478847884, -0.4387755102040816}, {0,
0.6947494749474946, -0.4387755102040816}}, {{0, \
-0.503980398039804, -0.41836734693877553}, {0, \
-0.26665666566656665, -0.41836734693877553}, {0,
0.49368936893689364, -0.41836734693877553}}, {{0, \
-0.491029102910291, -0.3979591836734694}, {0, \
-0.23494349434943493, -0.3979591836734694}, {0,
0.5065706570657065, -0.3979591836734694}}, {{0, \
-0.47926792679267927, -0.37755102040816324}, {0, \
-0.207010701070107, -0.37755102040816324}, {0,
0.5184018401840184, -0.3775510204081633}}, {{0, \
-0.4681368136813681, -0.35714285714285715}, {0, \
-0.18264826482648266, -0.35714285714285715}, {0,
0.5296029602960296, -0.35714285714285715}}, {{0, \
-0.45728572857285726, -0.33673469387755106}, {0, \
-0.16143614361436143, -0.33673469387755106}, {0,
0.5404540454045405, -0.33673469387755106}}, {{0, \
-0.4464346434643464, -0.3163265306122449}, {0, \
-0.14274427442744272, -0.3163265306122449}, {0,
0.5514451445144515, -0.3163265306122449}}, {{0, \
-0.4354435443544354, -0.29591836734693877}, {0, \
-0.12622262226222622, -0.29591836734693877}, {0,
0.5625762576257626, -0.29591836734693877}}, {{0, \
-0.42389238923892386, -0.2755102040816327}, {0, \
-0.1113111311131113, -0.2755102040816327}, {0, 0.5741974197419741,
-0.2755102040816327}}, {{0, -0.4116411641164116, \
-0.25510204081632654}, {0, -0.09786978697869786, \
-0.25510204081632654}, {0,
0.5865186518651865, -0.25510204081632654}}, {{0, \
-0.3984798479847985, -0.23469387755102042}, {0, \
-0.0854085408540854, -0.23469387755102045}, {0,
0.5998199819981997, -0.23469387755102042}}, {{0, \
-0.38405840584058404, -0.2142857142857143}, {0, \
-0.07378737873787378, -0.2142857142857143}, {0,
0.6143114311431143, -0.21428571428571433}}, {{0, \
-0.3680968096809681, -0.1938775510204082}, {0, \
-0.06279627962796279, -0.1938775510204082}, {0,
0.6304130413041305, -0.1938775510204082}}, {{0, \
-0.3501050105010501, -0.17346938775510207}, {0, \
-0.05222522252225223, -0.17346938775510207}, {0,
0.6484048404840485, -0.17346938775510207}}, {{0, \
-0.3297329732973297, -0.15306122448979592}, {0, \
-0.04186418641864187, -0.15306122448979592}, {0,
0.6689168916891688, -0.15306122448979592}}, {{0, \
-0.3062106210621062, -0.13265306122448983}, {0, \
-0.031503150315031536, -0.13265306122448983}, {0,
0.6924392439243924, -0.13265306122448983}}, {{0, \
-0.27904790479047903, -0.11224489795918369}, {0, \
-0.020862086208620845, -0.11224489795918369}}, {{0, \
-0.24782478247824782, -0.09183673469387761}, {0, \
-0.009520952095209534, -0.0918367346938776}}, {{0, \
-0.21226122612261225, -0.07142857142857145}, {0,
0.0031503150315032036, -0.07142857142857145}}, {{0, \
-0.17305730573057304, -0.05102040816326536}, {0,
0.01806180618061809, -0.05102040816326536}}, {{0, \
-0.13238323832383236, -0.030612244897959218}, {0,
0.0368236823682368, -0.030612244897959218}}, {{0, \
-0.09401940194019402, -0.010204081632653073}, {0,
0.061746174617461735, -0.010204081632653073}}, {{0, \
-0.061746174617461735, 0.010204081632653073}, {0,
0.0940894089408941,
0.010204081632653073}}, {{0, -0.03696369636963698,
0.030612244897959107}, {0, 0.13259325932593258,
0.030612244897959107}}, {{0, -0.01813181318131818,
0.05102040816326525}, {0, 0.17326732673267328,
0.05102040816326525}}, {{0, -0.0032203220322032378,
0.0714285714285714}, {0, 0.21240124012401237,
0.0714285714285714}}, {{0, 0.00938093809380941,
0.09183673469387754}, {0, 0.24796479647964795,
0.09183673469387754}}, {{0, 0.02072207220722072,
0.11224489795918358}, {0, 0.27918791879187915,
0.11224489795918358}}, {{0, -0.6894989498949894,
0.13265306122448972}, {0, 0.031433143314331446,
0.13265306122448972}, {0, 0.30621062106210617,
0.13265306122448972}}, {{0, -0.6661866186618661,
0.15306122448979587}, {0, 0.04172417241724172,
0.15306122448979587}, {0, 0.3295929592959296,
0.15306122448979587}}, {{0, -0.6459545954595459,
0.173469387755102}, {0, 0.05208520852085208,
0.173469387755102}, {0, 0.3499649964996499,
0.173469387755102}}, {{0, -0.6282428242824282,
0.19387755102040818}, {0, 0.06265626562656264,
0.19387755102040816}, {0, 0.3678867886788678,
0.19387755102040816}}, {{0, -0.6125612561256125,
0.2142857142857142}, {0, 0.07357735773577356,
0.2142857142857142}, {0, 0.38377837783778374,
0.2142857142857142}}, {{0, -0.5982798279827982,
0.23469387755102034}, {0, 0.08519851985198519,
0.23469387755102034}, {0, 0.39819981998199816,
0.23469387755102034}}, {{0, -0.5853285328532853,
0.2551020408163265}, {0, 0.09758975897589758,
0.2551020408163265}, {0, 0.41136113611361136,
0.2551020408163265}}, {{0, -0.5732873287328734,
0.2755102040816326}, {0, 0.11110111011101109,
0.2755102040816326}, {0, 0.42361236123612356,
0.2755102040816326}}, {{0, -0.5618061806180618,
0.29591836734693866}, {0, 0.126012601260126,
0.29591836734693866}, {0, 0.43516351635163514,
0.29591836734693866}}, {{0, -0.5508150815081508,
0.3163265306122448}, {0, 0.1426042604260426,
0.3163265306122448}, {0, 0.4462946294629463,
0.3163265306122448}}, {{0, -0.54003400340034,
0.33673469387755095}, {0, 0.16136613661366134,
0.33673469387755095}, {0, 0.45714571457145714,
0.33673469387755095}}, {{0, -0.5291829182918292,
0.35714285714285704}, {0, 0.18264826482648264,
0.3571428571428571}, {0, 0.46799679967996793,
0.35714285714285704}}, {{0, -0.5181218121812181,
0.3775510204081633}, {0, 0.20715071507150712,
0.37755102040816324}, {0, 0.4791279127912791,
0.37755102040816324}}, {{0, -0.5064306430643064,
0.3979591836734693}, {0, 0.2352235223522352,
0.3979591836734693}, {0, 0.4908890889088908,
0.3979591836734693}}, {{0, -0.4934793479347934,
0.4183673469387754}, {0, 0.2669366936693669,
0.4183673469387754}, {0, 0.5039103910391038,
0.4183673469387754}}, {{0, -0.6962896289628961,
0.43877551020408156}, {0, -0.4786378637863786,
0.43877551020408156}, {0, 0.30215021502150213,
0.43877551020408156}, {0, 0.5187518751875188,
0.43877551020408156}}, {{0, -0.6586258625862587,
0.4591836734693877}, {0, -0.46078607860786075,
0.4591836734693877}, {0, 0.33953395339533954,
0.4591836734693877}, {0, 0.5366736673667366,
0.4591836734693877}}, {{0, -0.6213821382138214,
0.47959183673469385}, {0, -0.4383138313831383,
0.47959183673469385}, {0, 0.37663766376637664,
0.47959183673469385}, {0, 0.559005900590059,
0.47959183673469385}}, {{0, -0.5874987498749875,
0.4999999999999999}, {0, -0.4101010101010101,
0.4999999999999999}, {0, 0.41031103110311024,
0.49999999999999983}, {0, 0.5871487148714871,
0.49999999999999983}}};

ListPointPlot3D[#,
PlotRange -> {{-0.5, 0.5}, {-0.7, 0.7}, {-0.5, 0.5}},
AxesLabel -> {"x", "y", "z"}, BoxRatios -> {1, 1, 2},
ImageSize -> Medium] &@Flatten[data2, 1]


d2 = FindClusters[Flatten[data, 1], 5, Method -> "SpanningTree"];
ListPointPlot3D[d2
, PlotRange -> {
{-0.5, 0.5}
, {-0.7, 0.7}
, {0, 1}
}
, AxesLabel -> {"x", "y", "z"}
, BoxRatios -> {1, 1, 2}
, ImageSize -> Medium
]


To draw lines connecting these components:

Graphics3D[{
Thick,
{ColorData[97][#], Line[d2[[#]] ]} & /@ Range@Length@d2
}]

• Thank you for suggesting this method. Sorry, I added another data2, which does not work well with FindClusters[Flatten[data2, 1], 6, Method -> "SpanningTree"]. I used to thought the old data with U-shape may be more difficult... Is it possible to make the new one work as well? Sep 10, 2022 at 5:57
• @xiaohuamao Changing the question substantially after an answer has already been provided is not good practice. As happened in this case, you risk invalidating the present answers and wasting peoples time. It makes it less likely that people will spend time trying to answer your question if it's a moving target. Sep 10, 2022 at 13:14
• @MarcoB I was originally thinking if a general solution is possible for this kind of problem and made a guess for MWE in order to make it concise. The two data sets are all I need, actually. You see, I tried to give MWE in order to save people's time. Unfortunately, the method is not general enough yet, or maybe it could be improved. It's rather due to a mismatch between what I thought difficult and what the proposed method is good at, which could be hard to predict. It just happens sometimes. Sep 10, 2022 at 14:03
• @MarcoB I certainly don't randomly change the question. Even if the solution cannot be that general, it is still definitely useful and helpful to people (including me) for sure. I'm just risking finding a better solution for this kind of problem. If none, we face it and thank the answerer. Sep 10, 2022 at 14:05
• @MarcoB No offence. People here have helped me a lot and I always appreciate that and don’t take it for granted. Just think it’s probably not that bad to modify the the example in order to better present the general problem of finding lines. Sep 10, 2022 at 14:23