6
$\begingroup$

Mathematica noob here. So I have a list of values (lvalues) which are real numbers, some positive, some negative. I want to use ListLinePlot to plot them, but I want the line to become red when goes to the negative values (meaning, the line is below the x-axis), and blue when it's positive (the line is above the x-axis). I did some little research here, but I found some solutions for coloring the points. A code (modified to my case) I found was

ListLinePlot[MapAt[Style[#, If[# > 0, Blue, Red]] &, {All}]@lvalues

The problem with this is that it is just color of the points, not the line itself, so I got the blue and red points, but the line still is a single color. Is there a way to color just the line (not the points) when it comes to ListLinePlot? (The lvalues come from experimental data and can't be reproduced by a function) Thanks!

$\endgroup$
0

2 Answers 2

11
$\begingroup$
lvalues = {1, 1, 2, 3, -5, 8, -4, 8}
ListLinePlot[lvalues, MeshFunctions -> {#2 &}, Mesh -> {{0}}, 
 MeshShading -> {Red,Blue}, MeshStyle -> Transparent]

enter image description here

$\endgroup$
4
$\begingroup$

Using RegionFunction:

Clear["Global`*"];
lvalues = {1, 1, 2, 3, -5, 8, -4, 8};
if = Interpolation[lvalues, InterpolationOrder -> 1]

Show[
 Plot[if[x], {x, 0, Length@lvalues}
  , PlotRange -> {Min@lvalues, Max@lvalues}
  , PlotStyle -> Blue
  , RegionFunction -> (#2 > 0 &)
  ],
 Plot[if[x], {x, 0, Length@lvalues}
  , PlotRange -> {Min@lvalues, Max@lvalues}
  , PlotStyle -> Red
  , RegionFunction -> (#2 < 0 &)
  ]
 ]

enter image description here

$\endgroup$

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.