3
$\begingroup$

I have constructed a graph using ListLinePlot, using the existing input data from the matrix calculation.

May I ask how can I create a different segment based on existing graph. The graph x-axis ranges from 0 to 540. I need to create three segments, based on the existing graph that I have.

enter image description here

So between 0 to 540, I need help in creating three segments from the existing data to 0-220, 221-421, 422 to 540. I have tried using DataRange, but the outcomes are still the same.

Appreciate your help. Thanks

$\endgroup$
2
  • 1
    $\begingroup$ I'm having a hard time understanding what do you want to achieve. Please show some exemplary dataset, the plot, and describe in more detail what's the output you want to obtain. $\endgroup$
    – corey979
    Commented Jan 12, 2017 at 10:53
  • $\begingroup$ Thanks corey979, I have updated the question. $\endgroup$
    – ljsg
    Commented Jan 13, 2017 at 1:50

1 Answer 1

4
$\begingroup$
lst1 = Accumulate[RandomReal[{-1, 1}, 540]];

1. Restructure your input data:

lst2 = MapIndexed[{#2[[1]], #} &, lst1];

{data1, data2, data3} = {lst2[[;; 220]], lst2[[221 ;; 421]], lst2[[422 ;;]]};

ListLinePlot[{data1, data2, data3} ]

Mathematica graphics

2. Use Mesh and MeshShading:

ListLinePlot[lst1, Mesh -> {{221, 422}}, MeshShading -> {Red, Blue, Green}]

Mathematica graphics

3. Use ColorFunction that specifies different colors for different values of x:

ListLinePlot[lst1, ColorFunctionScaling -> False, 
 ColorFunction->Function[{x, y}, Which[x <= 220, Orange, 221 <= x < 421, Cyan, True, Red]]]

Mathematica graphics

4. Plot each piece separately with appropriate DataRange values and combine with Show:

ranges = {{1, 220}, {221, 421}, {422, 540}};
colors = {Red,  Green, Purple};

plots = ListLinePlot[lst1[[Span @@ #]],  DataRange -> #, 
     PlotStyle -> #2] & @@@ Transpose[{ranges, colors}];
Show[plots, PlotRange -> All]

Mathematica graphics

5. Post-process ListLinePlot output to add different directives for different pieces:

ListLinePlot[lst1] /. 
 Line[x_] :> ({#, Line[x[[Span @@ #2]]]} & @@@ Transpose[{colors, ranges}])

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Thanks Kglr! It was really useful! I will give a try. Appreciate it! $\endgroup$
    – ljsg
    Commented Jan 13, 2017 at 8:55

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.