5
$\begingroup$

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:

enter image description here

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

enter image description here

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.

$\endgroup$
4
  • $\begingroup$ Look up ColorFunctionScaling and ColorFunction. $\endgroup$
    – Szabolcs
    Jul 16, 2018 at 11:08
  • $\begingroup$ 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? $\endgroup$
    – Kuba
    Jul 16, 2018 at 11:49
  • $\begingroup$ 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. $\endgroup$
    – MicheleG
    Jul 16, 2018 at 11:58
  • $\begingroup$ A related question. $\endgroup$ Sep 26, 2018 at 7:50

2 Answers 2

5
$\begingroup$

(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:

Mathematica graphics

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – MicheleG
    Jul 16, 2018 at 12:13
4
$\begingroup$

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:

Mathematica graphics Mathematica graphics

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

$\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.