2
$\begingroup$

I have the following MatrixPlot for which I want to specify a custom color function:

MatrixPlot[RandomReal[1, {10, 10}], ColorFunction -> customColorFunction]

The function should behave as follows: White only for a value smaller than 0.2. Then for the range of >0.2 to 0.7 it should be a continuous blend from red to blue and for the range >0.7 it should be black.

Is it possible to specify something like this with Blend or some other function?

$\endgroup$
4
$\begingroup$

In:

Clear[customColorFunction]
customColorFunction[x_] := Piecewise[{
   {White, x <= 0.2}, {Blend[{Red, Blue},
     (x - 0.2)/0.5], 0.2 < x <= 0.7},
   {Black, True}}]
Table[customColorFunction[x], {x, 0.01, 1, 0.01}]

SeedRandom[1]
m = RandomReal[{0, 1}, {10, 10}];

MatrixPlot[m, ColorFunction -> customColorFunction, 
 ColorFunctionScaling -> False]
ArrayPlot[m, ColorFunction -> customColorFunction]

Out: enter image description here

$\endgroup$
  • $\begingroup$ Just a quick question. Does any white blocks ever appear? $\endgroup$ – pfactors May 6 '17 at 13:37
  • $\begingroup$ Awesome!Could learn a lot from that. Thank you. $\endgroup$ – holistic May 6 '17 at 13:38
  • $\begingroup$ @pfactors ColorFunctionScaling is enabled in default. I disabled it. It's better to use ArrayPlot instead. $\endgroup$ – UnchartedWorks May 6 '17 at 13:57
  • $\begingroup$ Okay Uncharted. I found ArrayPlot better myself. $\endgroup$ – pfactors May 6 '17 at 14:00
2
$\begingroup$

I found that ArrayPlot kinda works a little better for me.

ArrayPlot[RandomReal[1, {10, 10}], ColorFunction -> Function[{c},
Which[c < 0.2, White, (c >= 0.2 && c <= 0.7), 
Blend[{Red, Blue}, (c - 0.2)*2], c > 0.7, Black]]]

enter image description here

$\endgroup$

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.