3
$\begingroup$

I have the following code:

ListLinePlot[{{0, 25}, {1, 150}, {2, 150}, {3, 0}, {4, 150}, {5, 
   150}, {6, 0}, {7, 200}},
 
 PlotStyle -> {{Blue}, {Orange}, {Blue, 
    Dashed}, {Orange}, {Green}, {Green}, {Black}, {Red}},
 
 PlotRange -> {All, {0, 250}}
 
 ]

How can I make each plot line in the plot (7 in total) to have a different color or dashes?. I am trying to accomplished that using PlotStyle -> {{Blue}, {Orange}, {Yellow, Dashed}, {Orange}, {Green}, {Brown}, {Black}, {Red}} but it does not work.

$\endgroup$
2
  • 1
    $\begingroup$ You'll have to construct it by hand. ListLinePlot does not consider the segments as separate lines, as you can see for yourself by running: Cases[yourListLinePlot, _Line, All] . There is only one Line object in the results. $\endgroup$
    – MarcoB
    Mar 5 at 20:15
  • $\begingroup$ @ MarcoB thanks! I see. But how can i individually construct it? $\endgroup$
    – John
    Mar 5 at 20:17
7
$\begingroup$
data = {{0, 25}, {1, 150}, {2, 150}, {3, 0}, {4, 150}, {5, 150}, {6, 0}, {7, 200}};

styles = {{Blue}, {Orange}, {Blue, Dashed}, {Orange}, {Green}, {Green}, {Black}, {Red}};

ListLinePlot[Partition[data, 2, 1],
 PlotStyle -> styles, 
 PlotRange -> {All, {0, 250}}]

enter image description here

Alternatively, you can use Mesh + MeshShading:

ListLinePlot[data, 
 Mesh -> {data[[All, 1]]}, 
 MeshShading -> Prepend[None][Directive /@ styles], 
 PlotRange -> {All, {0, 250}}]

enter image description here

Finally, you can use Graphics as follows:

Graphics[Thread[{Most[Directive /@ styles], 
   Line /@ Partition[data, 2, 1]}], 
 PlotRange -> {All, {0, 250}}, 
 AspectRatio -> 1 / GoldenRatio, 
 Axes -> True]

enter image description here

Graphics[GraphicsComplex[data, 
  Thread[{Most[Directive /@ styles], Line /@ Partition[Range@Length@data, 2, 1]}]], 
 PlotRange -> {All, {0, 250}}, 
 AspectRatio -> 1/GoldenRatio, 
 Axes -> True]
 same picture
$\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.