3
$\begingroup$

I have a CSV file that specifies three real numbers x, y, v, and I need to generate a plot (ListPlot, joined) with the lines colored according to the value of v.

But let's consider the simplest case, where v can be +1 or -1. Where values are +1 the line should be green, and blue where v is -1 (or solid/dotted).

For instance:

0, 0, +1 `
1, 1, +1 | < GREEN
2, 4, +1 /
3, 2, -1 ` < BLUE
4, 1, -1 /
5, 4, +1 ` < GREEN 

Is there a way of using the values and produce a plot? The questions I've found are about using a color function, but in this case I have no function as such.

$\endgroup$
3
$\begingroup$
list = {{0, 0, 1}, {1, 1, 1}, {2, 4, 1}, {3, 2, -1}, {4, 1, -1}, {5, 4, 1}};

1. You can Partition the input list to create separate lines and use the option PlotStyle to color pieces individually:

ListLinePlot[Partition[list[[All, ;; 2]], 2, 1], 
 PlotStyle -> (Rest[list[[All, -1]]] /. {1 -> Green, -1 -> Blue}) ]

enter image description here

2. You can use the options Mesh and MeshShading as follows:

split = SplitBy[list, Last];
mesh = split[[All, -1, 1]];
meshshading =split[[All, 1, -1]] /. {1 -> Green, -1 -> Blue};

ListLinePlot[list[[All, {1, 2}]], Mesh -> {mesh}, MeshShading -> meshshading]

enter image description here

| improve this answer | |
$\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.