# Set a fixed colour scale with DensityPlot

I am trying to plot some Zernike polynomials, but I want all of them to be plotted by using the same colour scale from -1 to +1 (i.e. the range of these polynomials). I am using the following code:

DensityPlot[
ZernikeR[n, m, Norm[{x, y}]] Cos[m ArcTan[x, y]], {x, -1, 1}, {y, -1,1}
, PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}}
, ColorFunction -> (Hue[2 (1 - #1)/3] &)
, PlotPoints -> 200
, PlotLegends -> Automatic
, RegionFunction -> Function[{x, y, z}, -1 < x^2 + y^2 < 1]
]


I get what I want with some polynomials, e.g. when m=0 and n=2:

and not with others, e.g. when m=0 and n=4:

where the colour range goes down to -0.5 and not to -1. I want all polynomials to be coloured with a scale from -1 to 1, from blue to red. I haven't found a way to fix this. What am I missing? Thanks.

• Look up ColorFunctionScaling and ColorFunction. Jul 16 '18 at 11:08
• ColorFunctionScaling -> False will prevent scaling but 2 (1 - #1)/3 &@Interval[{-1, 1}] gives: Interval[{0, 4/3}] which can be missleading when Hue is applied ( {0,1} base domain). What exactly do you want to achieve, how your manual rescaling is related to the problem and are you only concerned about colors or the range in the bar legend aswell?
– Kuba
Jul 16 '18 at 11:49
• I'd like the color scale to always vary between blue set for -1 and red set for +1, independently from the min and max values of the function in that range. Jul 16 '18 at 11:58
• A related question. Sep 26 '18 at 7:50

(This answer will be very similar to halirutan's answer (+1). The difference is that I preserved the color function from the OP, and that I made a fixed bar legend.)

We can do it like in this answer, which is to say, we can turn off the color function scaling and scale the values ourselves in a way that is the same for all plots. We can make our own bar legend that matches this scaling. We only need to change three options:

ColorFunction -> (Hue[2 (1 - Rescale[#1, {-1, 1}])/3] &),
ColorFunctionScaling -> False,
PlotLegends -> BarLegend[{Hue[2 (1 - Rescale[#1, {-1, 1}])/3] &, {-1, 1}}]


Now we get plots like this:

• Hi C.E., this is exactly the solution I just found thanks to the link you posted before. Thanks to all of you guys for you help. Jul 16 '18 at 12:13

You have currently two mistakes. The first one is your color-function itself. It needs to give Hue[0] for a value of -1 and Hue[1] for a value of 1. The transformation is

col[v_] := Hue[Rescale[v, {-1, 1}]]


Secondly, you need to turn off that Mathematica rescales the values for each plot which can be done with the ColorFunctionScaling -> False option.

plot[m_, n_] :=
DensityPlot[
ZernikeR[n, m, Norm[{x, y}]] Cos[m ArcTan[x, y]], {x, -1,
1}, {y, -1, 1}, PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}},
ColorFunction -> col, PlotPoints -> 200, PlotLegends -> Automatic,
ColorFunctionScaling -> False,
RegionFunction -> Function[{x, y, z}, -1 < x^2 + y^2 < 1]]


And then, plot[0,2] and plot[0,4] will use the same scaling for the colors:

If you don't want a circular color scheme like Hue is (begins and ends in red), then use one of the other schemes.