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$

1 Answer 1

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

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.