Is there a way to make 0 values a different color in a MatrixPlot?
SeedRandom[1]
outtabletoprint = RandomInteger[{0, 5}, {20, 20}];
1. You can replace 0
s in the input matrix with Black
:
MatrixPlot[outtabletoprint /. 0 -> Black,
ColorFunction -> "TemperatureMap", PlotLegends -> Automatic]
2. You can use the option ColorRules
:
MatrixPlot[outtabletoprint, ColorRules -> {0 -> Black},
ColorFunction -> "TemperatureMap", PlotLegends -> Automatic]
same picture
Note: Both methods work in version 9.0.1 (Windows 10 - 64 bit), in version 11.3 (Windows 10 -64 bit) and in version 12.1 (Wolfram Cloud).
color[z_] := Which[z == 0, Black, 0 < z <= 1, ColorData["TemperatureMap"][Rescale[z, {0, 1}]]]
Legended[MatrixPlot[outtabletoprint2, ColorFunction -> color, ColorFunctionScaling -> False],
BarLegend[{"TemperatureMap", {0, 1}}]]
Or
MatrixPlot[outtabletoprint2, ColorFunction -> color,
ColorFunctionScaling -> False,
PlotLegends -> BarLegend[{"TemperatureMap", {0, 1}}]]
If you really want Yellow-Red color, you can use SolarColors
in reverse order.
color[z_] := Which[z == 0, Black, 0 < z <= 1, ColorData[{"SolarColors", "Reverse"}][Rescale[z, {0, 1}]]]
MatrixPlot[outtabletoprint2, ColorFunction -> color,
ColorFunctionScaling -> False,
PlotLegends -> BarLegend[{{"SolarColors", "Reverse"}, {0, 1}}]]