0
$\begingroup$

Hello and have a nice day!

I need to make MatrixPlot for some data. Here you can find a notebook with preprocessing raw data and some functions from below text.

I want all "Inf" values to be colored white, those greater than 13 to be black, and for the rest a color gradient will be applied, and in the full range, and not in half.

When I use

ColorRules -> {x_ /; x === Infinity -> White, x_ /; 13 < x -> Black }

it goes wrong

MatrixPlot with ColorRules

because range should be bigger

BarLegend[{"Rainbow", {0, 13}}]

The problem doesn't solved by writing customize ColorFunction:

myColorData[c_] := Function[Which[
# === Infinity, White,
# > 13, Black,
True, ColorData[c][N@##]
]];

with calling

MatrixPlot[Flatten[bottleneck, 1], PlotRange -> {0, 13},ColorFunction -> (myColorData["ThermometerColors"][##] &), ColorFunctionScaling -> False]

Thank you! With respect!

$\endgroup$
2
  • 2
    $\begingroup$ Since you're turning off ColorFunctionScaling, you need to manually rescale. Try ...ColorRules -> {\[Infinity] -> White, x_ /; x > 13 -> Black}, ColorFunction -> (ColorData["Rainbow"][Rescale[N@#, {Min@Flatten@bottleneck, 13}, {0, 1}]] &), ColorFunctionScaling -> False... $\endgroup$
    – N.J.Evans
    Sep 18 at 16:42
  • $\begingroup$ @n-j-evans thank you for your response $\endgroup$
    – Aleks
    Sep 18 at 18:07

1 Answer 1

5
$\begingroup$

I couldn't use the numbers in your file, so here is my best effort with random data.

Clear["Global`*"];
SeedRandom[1];
(amat = RandomChoice[
    Range[20]~Join~{∞}, {30, 30}]) // MatrixPlot

col[x_] := 
  If[x == ∞, White, 
   If[x > 13, Black, ColorData["Rainbow"][Rescale[x, {1, 13}]]]];

MatrixPlot[amat
 , ColorFunction -> col
 , ColorFunctionScaling -> False
 , PlotLegends -> {
   Placed[BarLegend[{col[#] &, {0, 14}}
     , LegendMarkerSize -> 134], {1.01, 0.4}]
   , Placed[SwatchLegend[{Black, White}, {"x>13", "∞"}
     , LegendLayout -> "ReversedColumn"
     , LegendMarkerSize -> 15
     ], {1.025, 0.7}]
   }
 ]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Thank you, dear Syed! 🙏🏻 $\endgroup$
    – Aleks
    Sep 18 at 17:38

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.