# How do I specify an exact value range in ColorFunction?

Posted 8 years ago
26605 Views
|
6 Replies
|
9 Total Likes
|
 I'm trying to display running cross-correlograms with ListDensityPlot (with time lag on y axis, time on x-axis, and color corresponding to correlation coefficient). I want to use the "Rainbow" color function, such that violet corresponds to a correlation coefficient of -1 and red to +1. I want to set this absolute scale for many different ListDensityPlots so I can directly compare cross-correlograms, but ColorFunction normalizes the data in each plot to run from 0 to 1. I understand I can disable this normalization with ColorFunctionScaling->False, but I still do not know how to establish an absolute color scale that I can use in different plots. Does anyone know how to write such an explicit ColorFunction? Thank you. Answer
6 Replies
Sort By:
Posted 8 years ago
 Something like this would do the trick.Plot[0.8 Sin[x] + 0.2, {x, 0, 3 \[Pi]}, ColorFunction -> (ColorData["Rainbow"][Rescale[#2, {-1, 1}]] &), ColorFunctionScaling -> False, PlotRange -> {-1, 1}]  Answer
Posted 8 years ago
 This is Sander's idea applied to ListDensityPlotTable[ ListDensityPlot[  Table[k Sin[3 x + y^2], {x, -3, 3, 0.1}, {y, -3, 3, 0.1}],   ColorFunction -> (ColorData["Rainbow"][Rescale[#, {-1, 1}]] &),   ColorFunctionScaling -> False, PlotRange -> {-1, 1}] , {k, {.5, 1}}]  Answer
Posted 8 years ago Answer
Posted 3 months ago
 Could anyone explain this statement: Rescale[#, {-1, 1}, {380, 750}]] &)What is # and &)? Answer
Posted 3 months ago
 Lookup the function Function in the documentation: https://reference.wolfram.com/language/ref/Function.htmlThere are several notations one of them with # and &, basically # will be the 'input" and & denotes that it is a "pure" or lambda function. Answer
Posted 8 years ago
 Just a tiny little more info. In case that you need other color schemes for actual spectrum with physical meaning, you can go to the menu bar and click "Palettes" -> "Color Scheme" ->"Physical " -> "Visible spectrum".  You can see that the default range for this color data is not from 0 to 1 but from 380 to 750, which corresponding to the physical wave length. Thus you just need to add a color range in to the Rescale function and it will convert the range of your function to that of the visible spectrum. This is very handy extension to the default cases above. Here is the workable code:k = 0.5;ListDensityPlot[ Table[k Sin[3 x + y^2], {x, -3, 3, 0.1}, {y, -3, 3, 0.1}], ColorFunction -> (ColorData["VisibleSpectrum"][     Rescale[#, k*{-1, 1}, {380, 750}]] &), ColorFunctionScaling -> False, PlotRange -> {-1, 1}] Answer