1
$\begingroup$

Observe the following code:

nd[y_, a_, b_, n_] := (Binomial[b - a + 1, y]*Sum[((-1)^i)*Binomial[y, i]*((y - i)/(b - a + 1))^n, {i, 0, y}]);
mycolorfun = Function[Blend[{Black, Purple, Blue, Green, Yellow, Orange, Red, White}, Rescale[#, {0, 1}]]]
Manipulate[
 ArrayPlot[Table[nd[y, 1, K, n], {K, 1, 25}, {n, 1, 25}], 
  ColorFunction -> mycolorfun, DataReversed -> True, Frame -> True, 
  FrameTicks -> Automatic, 
  FrameLabel -> {Rotate["K", -90 Degree], "N"}], {y, 1, 15, 1}]

Notice how when the slider is moved up to y=15, there is a white square that appears at N = 25 and K = 21:

enter image description here

My goal is for the color function to map the individual value of the output of my function to a color as dependent on its closeness to 0 or 1 (since my function is a probability density function, this makes sense).

However, look what happens when you check the actual value of the function for N = 25 and b=K=21:

nd[15, 1, 21, 25.0]
(out) 0.268495

I get 26%! 26% should be mapping to a color somewhere around Purple/Blue, but it's getting mapped all the way up to the color white! White is what's supposed to be mapped to when the output of my function is really close to 1! Clearly something is amiss here, and I cannot figure out what it is. Can somebody please explain what is going on and what I need to do to get the desired color output?

$\endgroup$
  • $\begingroup$ Please include a definition for mycolorfun. $\endgroup$ – JimB Nov 19 '17 at 18:51
  • $\begingroup$ Thanks, I have added it in. I didn't realize I accidentally missed that part. $\endgroup$ – ereHsaWyhsipS Nov 19 '17 at 18:57
  • $\begingroup$ You should avoid using N as a variable name as that is a function in Mathematica. You're getting "white" because that is where the largest value in your table is. Someone else will know what the function for having a constant scaling from 0 to 1 will be. $\endgroup$ – JimB Nov 19 '17 at 19:09
  • $\begingroup$ I appreciate the advice for the N variable name. So I see what you're getting at, where it maps to white because that's the largest value that's appearing in the table. So do you have any ideas for how I could get around that so that the value directly maps to the color? $\endgroup$ – ereHsaWyhsipS Nov 19 '17 at 19:24
3
$\begingroup$

Short answer: Add ColorFunctionScaling -> False.

From my understanding, the ColorFunction option takes all the arguments supplied to it and scales them so that they go from 0 to 1.

So, if you look at the max value in the table at y = 15,

N@Max[Table[nd[15, 1, K, n], {K, 1, 25}, {n, 1, 25}]]

you can see that it's 0.268495. ColorFunction will take this as its "1", which, using mycolorfun, corresponds to a reddish color.

I think most of the other non-black colors in your plot are, in that sense, "wrong" as well, but correct me if that seems off to you:

Unscaled ArrayPlot

Anyway, hopefully that helps!

| improve this answer | |
$\endgroup$
  • $\begingroup$ You're right, my other colors were wrong as well. However, that particular white block was what was the main indication to me. Thank you so much! $\endgroup$ – ereHsaWyhsipS Nov 19 '17 at 23:25
  • $\begingroup$ No problem! That's impressive; dunno if I would have caught that. $\endgroup$ – Anne Nov 19 '17 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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