1
$\begingroup$

I have some data points in given below format:

data = {{0, 0}, {0.5, 2}, {1, 5}, {2, 8.5}, {4, 10}, {0, 0}, {0.5, 
4}, {1, 7}, {2, 9.5}, {4, 12}, {0, 0}, {0.5, 5}, {1, 9}, {2, 
12}, {4, 18}};

ListLinePlot[data

How to plot these data in ListLinePlot without joining the line from last data point to first one (highlighted by yellow square)?

$\endgroup$
  • $\begingroup$ What code generated the plot? $\endgroup$ – Coolwater Jan 14 '18 at 11:32
  • $\begingroup$ ListLinePlot[data] $\endgroup$ – Gopal Verma Jan 14 '18 at 11:33
  • 1
    $\begingroup$ That makes no sense. The maximal y-value in data is 10, whereas on the plot it's 19. $\endgroup$ – Coolwater Jan 14 '18 at 11:41
  • 1
    $\begingroup$ Please post the full code that gave your output. With the given data, we can get this. $\endgroup$ – corey979 Jan 14 '18 at 12:07
  • $\begingroup$ Shall data describe a single curve or 3? (In the latter case, the three curves would be equal.) $\endgroup$ – Henrik Schumacher Jan 14 '18 at 12:09
6
$\begingroup$
ListLinePlot[Split[data, #2 =!= {0, 0} &], PlotMarkers -> Automatic]

myplot.jpeg

Edit

The problem with the 'real' data imported from drive.google.com/open?id=1uXuPcB-ibTS3Q7d4EDluyz-JF-wm_CLL is merely one of machine precision versus exact values for {0,0} - the y-values of each 'split point' are very close to but not exactly zero.

Mathematica has a very nice command, Chop, which 'replaces approximate real numbers in expr that are close to zero by the exact integer 0.", and one way of solving the problem is as follows:

ListLinePlot[Split[Chop[data, 0.0001], #2 =!= {0, 0} &], PlotMarkers -> Automatic]

mynewplot.jpeg

Data were imported with:

data = Import["glv.1to.5.txt", "Data"];
$\endgroup$
  • $\begingroup$ Thanks it working on data that is given here, but it is not working on .txt imported data file? $\endgroup$ – Gopal Verma Jan 14 '18 at 21:20
  • $\begingroup$ @GopalVerma Can you supply a copy of the .txt file? (and, if possible, the code you use to import). $\endgroup$ – user1066 Jan 14 '18 at 23:26
  • $\begingroup$ Pls. find the link of data file. I am using Import[filename.txt,"Data".] command for importing the data file. drive.google.com/open?id=1uXuPcB-ibTS3Q7d4EDluyz-JF-wm_CLL $\endgroup$ – Gopal Verma Jan 15 '18 at 16:52
  • $\begingroup$ Thanks,It is working. $\endgroup$ – Gopal Verma Jan 15 '18 at 22:17
  • $\begingroup$ Can u suggest how to change the thickness of line joining the data point(or plot markers). $\endgroup$ – Gopal Verma Jan 20 '18 at 16:08
5
$\begingroup$
data = {{0, 0}, {0.5, 2}, {1, 5}, {2, 8.5}, {4, 10}, {0, 0}, {0.5, 4}, {1, 7}, {2, 9.5}, {4, 12}, {0, 0}, {0.5, 5}, {1, 9}, {2, 12}, {4, 18}};

By splitting the data separate curves are obtained

ListLinePlot[TakeList[data,
                      Length /@ Split[Module[{i = 0, prev = -∞},
                       Table[If[prev < (prev = j), i, ++i], {j, data[[All, 1]]}]]]]]

Alternatively:

(* positions where new curve start *)
pos = Position[UnitStep[Differences[data[[All, 1]]]], 0][[All, 1]]

ListLinePlot[TakeList[data,
                      Append[Differences[Join[{0}, pos]], UpTo[∞]]]]
$\endgroup$
  • $\begingroup$ I think you meant Take[List ... ]. $\endgroup$ – Themis Jan 14 '18 at 14:02
  • 1
    $\begingroup$ @Themis No, TakeList is new in 11.2 $\endgroup$ – Coolwater Jan 14 '18 at 14:04
  • $\begingroup$ Thanks, But I don't have Math. 11.2 so can u suggest any other option for that. $\endgroup$ – Gopal Verma Jan 14 '18 at 14:52
  • $\begingroup$ @Coolwater Good to know, I am still in 11.1.1. $\endgroup$ – Themis Jan 14 '18 at 15:07
  • $\begingroup$ @GopalVerma In older versions replace TakeList by Internal`PartitionRagged $\endgroup$ – Coolwater Jan 14 '18 at 15:20
5
$\begingroup$

Does

ListLinePlot[Partition[data, Floor[Length[data]/3]]]

do what you seek for?

$\endgroup$
  • $\begingroup$ Sorry,It is not working. $\endgroup$ – Gopal Verma Jan 14 '18 at 13:29
  • $\begingroup$ Joining line (highlighted by yellow square: updated figure) still present in graph. $\endgroup$ – Gopal Verma Jan 14 '18 at 13:37
  • 1
    $\begingroup$ @GopalVerma It does work. $\endgroup$ – corey979 Jan 14 '18 at 16:33
4
$\begingroup$

Another alternative is to insert a Null element where you want a break in the line:

data2 = {{0, 0}, {0.5, 2}, {1, 5}, {2, 8.5}, {4, 10}, Null, 
  {0, 0}, {0.5, 4}, {1, 7}, {2, 9.5}, {4, 12}, Null, 
  {0, 0}, {0.5, 5}, {1, 9}, {2, 12}, {4, 18}};
ListLinePlot[data2]

enter image description here

$\endgroup$
  • $\begingroup$ (+1). Or alternatively: ListLinePlot[Riffle[data, Null, 6]] $\endgroup$ – user1066 Jan 14 '18 at 19:28

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.