I will produce a large number of ListDensityPlot
s in which each entry can be positive or negative. I'd like to color positive values in green, negative values in red, and the value of $0$ as medium gray.
An off-the-shelf attempt at a solution is to use a ColorFunction
such as "RedGreenSplit"
, as here:
data = Table[Sin[j^2 + i] + .4, {i, 0, 2, .4}, {j, 0, 2, .4}];
ListDensityPlot[data, InterpolationOrder -> 0, ColorFunction -> "RedGreenSplit"]
There are three immediate problems, two fairly simple, the other hard (as far as I can see).
- The blending puts white in the middle of the range, while I seek gray (and thus blends from gray to green, and gray to red).
- The white is placed in the middle of the range of values; there is no guarantee that the value $0$ must correspond to gray. Notice that for the function I used, there is an overall offset ($0.4$) but the coloring does not respect that. (If I include
ColorFunctionScaling -> False
, gray need not correspond to points having value $0$.) - The maximum absolute value should be pure red or pure green, and the opposite color should be scaled the same. Thus if the range is $[-1,2]$ then the colors should go from a partial red/gray, through gray, up to full green. If instead the range is $[-6,3]$, then the colors should range between full red, through gray, up to partial green/gray.
Thus I want a color function that is Piecewise
, as in this question, but somehow the ranges must be set to ensure the colors.