6
$\begingroup$

The following code

ListLinePlot[{2, 4, 6, 8, 10, 12}, 
 ColorFunction -> Function[{x, y}, If[x <= 3, Blue, Red]]]

should produce blue lines for $x\le3$ and red lines for $x\ge3$. But instead, the plot contains only blue lines.

Where are my red lines?

$\endgroup$
3
  • 2
    $\begingroup$ ColorFunctionScaling $\endgroup$ Jun 18, 2012 at 18:05
  • $\begingroup$ Why should I use ColorFunctionScaling? My specific color function delivers Blue or Red – two well defined color directives. $\endgroup$
    – Deniz
    Jun 18, 2012 at 18:08
  • 2
    $\begingroup$ @Deniz B/c ColorFunctionScaling prevents the scaling of the argument, not of the function $\endgroup$ Jun 18, 2012 at 18:50

3 Answers 3

10
$\begingroup$

Try

ListLinePlot[{2, 4, 6, 8, 10, 12}, 
ColorFunction -> Function[{x, y}, If[x <= 3, Blue, Red]], 
ColorFunctionScaling -> False, PlotStyle -> Thick, Mesh -> All, 
MeshStyle -> {Orange, PointSize[0.03]}, PlotRange -> 13, 
Frame -> True]

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ Mesh is a substitute for PlotMarkers? Didn't know that! $\endgroup$
    – Verbeia
    Jun 18, 2012 at 20:25
  • $\begingroup$ How to make this color change according to the given values on the same line? For example, smaller values light color and greater values darker colors? $\endgroup$
    – Naps
    Oct 21, 2021 at 14:06
  • $\begingroup$ @Naps You can just use dark and light version of a single color. Change the ColorFunction in above code with a function like this Function[{x, y}, If[x <= 3, Lighter@Blue, Darker@Blue]] . $\endgroup$ Oct 24, 2021 at 12:55
6
$\begingroup$

Here's how to drive Brett's and belisarius's point home. Let's modify your original code a bit so we can see exactly what things are passed to your ColorFunction:

Reap[ListLinePlot[{2, 4, 6, 8, 10, 12},
     ColorFunction -> Function[{x, y}, Sow[x]; If[x <= 3, Blue, Red]]]]

We obtain your erroneous plot as the first component of a list, and the list of all the values passed to your ColorFunction: {{0., 0.2, 0.4, 0.6, 0.8, 1.}} as the second component. Effectively, what was done is to apply Rescale[] to the original list you plotted (i.e. Rescale[N@{2, 4, 6, 8, 10, 12}]). That's because ColorFunctionScaling is by default set to True:

Options[ListLinePlot, ColorFunctionScaling]
{ColorFunctionScaling -> True}

Let's see what happens if we disable ColorFunctionScaling:

ListLinePlot[{2, 4, 6, 8, 10, 12}, 
 ColorFunction -> Function[{x, y}, If[x <= 3, Blue, Red]], 
 ColorFunctionScaling -> False]

doubly-colored ListLinePlot

Note the nice purplish shade in between 3 and 4, showing that the color interpolation is working nicely.

$\endgroup$
1
  • $\begingroup$ How would one go about removing the color interpolation? $\endgroup$ Oct 4, 2016 at 19:51
1
$\begingroup$

You can also use Mesh + MeshShading

ListLinePlot[{2, 4, 6, 8, 10, 12}, MeshStyle -> PointSize[0], 
 Mesh -> {{3}}, MeshShading -> {Blue, Red}]

enter image description here

If you need to add PlotMarkers

Show[%, ListPlot[{2, 4, 6, 8, 10, 12}, PlotStyle->Green,  PlotMarkers-> {Automatic, 16}]]

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.