4
$\begingroup$

I want to plot a nested list using ListLinePlot and to set different colors according to the length of the nested lists, e.g.:

data={{0.5, 0.25, 0.75, 0.75, 1.25, 1., 1.5, 1., 1.5, 0.5, 1., 1., 1.5, 
  0.5, 1., 1., 1., 1.25, 1.25, 1., 1., 1.25, 1.25, 1.75, 1.75, 1.25, 
  1.25, 1., 1., 0.5, 0.5}, {0.25, 0.75, 1., 1., 0.5, 1., 0.5, 0.5, 
  0.75, 0.5, 0.75, 1.25, 0.75, 0.5, 0.}, {0.5, 0.75, 1.25, 0.5, 1., 
  0.5, 1., 1., 1., 1., 1., 1.25, 1.25, 0.5, 0.5}, {0.75, 0.5, 0.5, 
  0.5, 0.5, 0.75, 0.}, {0.5, 0.25, 0.75, 0.5, 1., 0., 0.5, 0.5, 0.5, 
  0.75, 0.75, 1., 1., 0.5, 0.5}, {0.25, 0.5, 0., 0., 0.25, 0.5, 
  0.}, {0.5, 0., 0.5, 0.5, 0.5, 0.5, 0.5}};

ListLinePlot[Flatten[data], PlotRange -> All]

so that each each of the nested lists has its own color:

color1range = Length[data[[1]]];
color2range = Length[data[[2]]];

Any ideas?

$\endgroup$
4
$\begingroup$

I'm assuming (from your use of Flatten) that you want all the sublists to be plotted sequentially, rather than stacked on top of each other. One way to achieve this is the following:

l = Accumulate[Join[{0}, Length /@ data]];
ListLinePlot[MapIndexed[{l[[#2[[1]]]] + #2[[2]], #1} &, data, {2}]]

which gives

enter image description here

(Basically, you have to build in the correct x-coordinate to your data.) You can then manipulate the colours of each sublist with PlotStyle (which I'll get to below).

One problem with this plot is that the lines don't join up. A more convoluted version of the same approach (which indexes the Flattened data and then partitions it back into the correct form) can fix this:

l = Length /@ data
data2 = MapIndexed[{First@#2, #1} &, Flatten[data]]
ind = Join[{1}, Accumulate[l]]
data3 = Table[data2[[ind[[j]] ;; ind[[j + 1]]]], {j, Length[ind] - 1}]

ListLinePlot[data3, PlotRange -> All]

which gives you

enter image description here

(I'm sure there's a cleaner way to do that. Partition for ragged arrays would help, but I think I'm missing something simple.)

Finally, if you want to colour to depend on the length of the sublist you could do:

l = Length /@ data;    
ListLinePlot[data3, PlotRange -> All, PlotStyle -> (Hue[#/Max[l], 0.75, 0.75] & /@ l)]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ There is Internal`PartitionRagged $\endgroup$ – corey979 Dec 18 '17 at 9:51
  • $\begingroup$ @corey979 Thanks! I can't remember how many times I've wished I had something like that. $\endgroup$ – aardvark2012 Dec 18 '17 at 10:23
2
$\begingroup$
colors = ColorData[63, "ColorList"][[;; Length@data]];
mesh = Accumulate[Length /@ data];

llp = ListLinePlot[Flatten@data, BaseStyle -> Thick, Frame -> True,
 MeshShading -> colors, MeshStyle -> None, Mesh -> {mesh}]

enter image description here

rectangles = Thread/@Thread[{Partition[Prepend[mesh, 0], 2, 1], {0, Max[data]}}, List, 1];

Show[llp, GridLines -> {mesh, None}, 
  Epilog -> ({Opacity[.2], #, Rectangle @@ #2} & @@@ Transpose[{colors, rectangles}])]

enter image description here

$\endgroup$
1
$\begingroup$

Another way of doing it:

ListLinePlot[Block[{i = 1}, Map[{i++, #} &, data, {2}]]]

enter image description here

To get rid of the gaps (and to show another method)

colors = ColorData[97, "ColorList"][[;; Length@data]];

clist = Most @ Flatten @ MapThread[ConstantArray, {colors, List@*Length /@ data}];

par = Partition[#, 2, 1]& @ Transpose[{Range@Length@#, #}] &[Flatten @ data];

Graphics[
 Transpose[{clist, Map[Line] @ par}],
 Axes -> True,
 AspectRatio -> 1/GoldenRatio,
 BaseStyle -> Thickness[0.005]]

enter image description here

$\endgroup$
0
$\begingroup$
ListPlot[data,
 PlotStyle -> {Red, Green, Blue, Yellow, Orange}]
$\endgroup$

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.