5
$\begingroup$

This question already has an answer here:

The following plot (A)

 ListLinePlot[{0,1,2,3},ColorFunction->"TemperatureMap"]

goes from blue to read.

The following plot (B)

ListLinePlot[{4,5,6,7},ColorFunction->"TemperatureMap"]

goes also from blue to read.

I want the colors to be correlated to the number to be plotted, not the position in the list (as above). Therefore I would like (A) to go from white to yellow (approx.) and (B) from yellow to red (approx.).

How can I make the color to be correlated with the value of the number to be plotted and not the position in the list?

$\endgroup$

marked as duplicate by Mr.Wizard Jul 15 '15 at 15:06

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.

  • $\begingroup$ Yeah....this is no duplicate. It's related, but not nearly a duplicate. $\endgroup$ – jvriesem Dec 8 '15 at 22:17
3
$\begingroup$

Your problem is due to the fact, that the default setting is ColorFunctionScaling -> True. You'll have to set ColorFunctionScaling -> False with the downside, that you'll now have to take care of a proper scaling:

ListLinePlot[{0, 1, 2, 3}, ColorFunction -> (ColorData["TemperatureMap", #2/7] &), 
 ColorFunctionScaling -> False]

Plot1

ListLinePlot[{4, 5, 6, 7}, ColorFunction -> (ColorData["TemperatureMap", #2/7] &), 
 ColorFunctionScaling -> False]

Plot2

$\endgroup$
  • 1
    $\begingroup$ how could I fix the ColorFunction, such that I can see a blue->green colour gradient in a y axes range of 0.05 (f. ex. -0.04<y<0.01). I tried to play around with the number multiplying #2, but I did not succeed. $\endgroup$ – Mencia Mar 27 '15 at 13:51
  • 2
    $\begingroup$ @Mencia ColorFunction -> (Blend[{Blue, Green}, (#2 + 0.04)/0.05] &) should give you the desired result. $\endgroup$ – Karsten 7. Mar 27 '15 at 14:18
  • $\begingroup$ Thanks great! Let say now, that I do ...Blend[{Blue,White,Green}... and that I want the center of the white span to be at y=0 always, i.e., to have a symmetric Colour Gradient around y=0 (White). How can I do this? $\endgroup$ – Mencia Mar 27 '15 at 14:28
  • 1
    $\begingroup$ @Mencia ColorFunction -> (Blend[{{-0.04, Blue}, {0, White}, {0.01, Green}}, #2] &)? $\endgroup$ – Karsten 7. Mar 27 '15 at 14:33
  • 1
    $\begingroup$ @Mencia Or ColorFunction -> (Blend[{{-0.04, Blue}, {0, White}, {0.04, Green}}, #2] &) depending on what you mean with symmetric. $\endgroup$ – Karsten 7. Mar 27 '15 at 14:35
3
$\begingroup$

Your plots has {1,2,3,4} and {1,2,3} for the values of x, and, unless an explicit function is used as the ColorFunction the default argument is taken to be x. Hence the same color in both of your plots. If you want the color to change with y you need to use a function that specifies that dependence:

 ListLinePlot[{0, 1, 2, 3}, ColorFunction -> "TemperatureMap", 
   PlotStyle -> Thickness[.02], 
   ColorFunction -> Function[{x, y}, ColorData["TemperatureMap"][y]]]
   (* or ColorFunction -> (ColorData["TemperatureMap", #2] &) *)

enter image description here

 ListLinePlot[{4, 5, 6}, ColorFunction -> "TemperatureMap", 
     PlotStyle -> Thickness[.02], 
     ColorFunction -> Function[{x, y}, ColorData["TemperatureMap"][y]]]

enter image description here

You can also use the same rescaled color function for both plots:

Row[ListLinePlot[#, PlotStyle -> Thickness[.02], PlotLabel -> #, 
     ImageSize -> 400, ColorFunctionScaling -> False, 
     ColorFunction -> Function[{x, y}, 
     ColorData[{"TemperatureMap", {-6, 6}}][y]]] & /@ 
  {{0, 1, 2, 3}, {4, 5, 6}}]

enter image description here

$\endgroup$

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