4
$\begingroup$

This question already has an answer here:

I am trying to style the line differently above a value, in this case 100, but when I use ColorFunction the line is blurred, showing a purple color near the 100 line. Ideally, it would switch from blue to red without any blur between.

data = {13.4, 16, 18.6, 23.2, 15.4, 19.2, 14.4, 17.2, 15.4, 19.6, 15, 20.8, 
250, 320, 630, 459.9, 655.2, 693, 567, 310, 300, 196, 24.4, 23.8, 17.2, 22, 12.6, 
15, 24.2, 13.6, 25.6, 16.2, 15.4, 23.2, 24.2, 18.2, 18, 23.4, 19.2, 13.2, 19, 
24.6, 20, 18.4, 21, 186, 485, 579.6, 422.1, 485.1, 598.5, 472.5, 480, 276, 192, 
12.8, 12.6, 19.4, 12.8, 23.4, 19.6, 15.8, 18, 18.8, 25.6, 152, 420, 541.8, 793.8, 
434.7, 441, 642.6, 355, 252, 210, 19, 24.8, 19.8, 16.4, 24.6, 16.4, 16.6, 15, 
20.6, 25.4, 20.8, 15, 15.8};

ListLinePlot[
    data, 
    ColorFunction -> Function[{x, y}, If[y > 100, Red, Blue]],
    ColorFunctionScaling -> False
]

Resultant Graph

I believe the reason for the blur is that no points lie very close the line, so the color function blurs the area between these points. If I add points to the data like 99, 101 the color change will be abrupt but it obviously changes the data as seen below in the first spike.Resultant Graph 2

Is there a way to add an abrupt color change without changing the data?

$\endgroup$

marked as duplicate by Rahul, m_goldberg, user31159, Wjx, gwr Oct 1 '16 at 12:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    $\begingroup$ Replace ColorFunction -> Function[{x, y}, If[y > 100, Red, Blue]], ColorFunctionScaling -> False with MeshFunctions -> {#2 &}, Mesh -> {{100}}, MeshShading -> {Red, Blue}, MeshStyle -> None. mathematica.stackexchange.com/q/59951/484 $\endgroup$ – Rahul Sep 30 '16 at 23:34
3
$\begingroup$

Here are a few hacks that achieve your result. This will at least let you do what you're going to do, but there may be more direct methods. You can plot the data twice with PlotRangeClipping->True over two different ranges and combine the two plots:

Show[
 ListLinePlot[
  data, PlotRangeClipping -> True
  , PlotRange -> {All, {0, 100}}
  , PlotStyle -> Blue
  ],
 ListLinePlot[
  data, PlotRangeClipping -> True
  , PlotRange -> {All, {100, Max@data}}
  , PlotStyle -> Red
  ]
 , PlotRange -> All
 ]

Which gives:

enter image description here

Or you can plot an interpolated list to fill in the gaps:

int = Table[
   {i, Interpolation[MapIndexed[{First@#2, #1} &, data], 
      InterpolationOrder -> 1][i]}
   , {i, 1, Length@data, 0.01}
   ];

ListLinePlot[
 int,
 ColorFunction -> Function[{x, y}, If[y > 100, Red, Blue]],
 ColorFunctionScaling -> False
 ]

Which gives:

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.