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$

2 Answers 2

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$
4
  • $\begingroup$ Just a quick question. Does any white blocks ever appear? $\endgroup$
    – foxcode64
    May 6, 2017 at 13:37
  • $\begingroup$ Awesome!Could learn a lot from that. Thank you. $\endgroup$
    – holistic
    May 6, 2017 at 13:38
  • $\begingroup$ @pfactors ColorFunctionScaling is enabled in default. I disabled it. It's better to use ArrayPlot instead. $\endgroup$
    – webcpu
    May 6, 2017 at 13:57
  • $\begingroup$ Okay Uncharted. I found ArrayPlot better myself. $\endgroup$
    – foxcode64
    May 6, 2017 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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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