9
$\begingroup$

I have a matrix plot that I create using the following code:

MatrixPlot[outtabletoprint,ColorFunction->"TemperatureMap",FrameTicks->{ticks,None,None,MapAt[Rotate[#,90 Degree]&,ticks,{All,2}]},PlotLegends->Automatic,RotateLabel->True,PlotLabel->Style[seqgroupname,FontSize->18]]

Since my data has many 0 values, I would like to show the 0 values as black to create a better contrast but I would like all other cells with values greater than 0 to use the "TemperatureMap" color function. The reason I would like to do this is because as you can see in the picture below it is hard to distinguish between low values and 0 values. How can I do this.

enter image description here

The code below works but it does not give a legend. I am using version 11.3 Windows 10 64 bit.

outtabletoprint2={{1.0,0,0.015,0,0.0074,0,0.11,0.089,0.0074,0.0074,0,0.022},{0,1.0,0,0,0.0069,0,0.014,0.021,0,0,0.0069,0.0069},{0.0076,0.0076,1.0,0.015,0.046,0.015,0,0.038,0.015,0.015,0.015,0},{0,0,0.0081,1.0,0.0081,0,0,0.065,0,0,0.0081,0},{0,0.014,0,0,1.0,0,0,0.014,0,0,0.46,0},{0,0,0,0,0,1.0,0,0.023,0,0,0,0},{0.13,0,0.015,0,0.0075,0,1.0,0.69,0,0,0,0.22},{0.10,0,0.022,0,0.0075,0,0.67,1.0,0,0.015,0.015,0.14},{0,0,0,0,0,0,0,0.0071,1.0,0.19,0,0},{0,0,0,0,0,0,0,0.0071,0.21,1.0,0,0},{0,0.014,0,0,0.45,0,0,0.0070,0,0,1.0,0},{0.037,0,0.022,0,0.0075,0,0.23,0.17,0,0,0,1.0}};
seqgroupname2="Example"
indexnames2={A,B,C,D,E,F,G,H,I,J,K,L};
(*This creates a variable to hold the tick mark names that define the categorical values of a heatmap*)
ticks2=Transpose[{Range[Length[indexnames2]],indexnames2}];

MatrixPlot[outtabletoprint2,ColorRules->{0->Black},ColorFunction->"TemperatureMap",FrameTicks->{ticks2,None,None,MapAt[Rotate[#,90 Degree]&,ticks2,{All,2}]},PlotLegends->Automatic,RotateLabel->True,PlotLabel->Style[seqgroupname2,FontSize->18]]

enter image description here

$\endgroup$
5
  • $\begingroup$ what do you get when you run MatrixPlot[outtabletoprint2 /. 0 -> Black, ColorFunction -> "TemperatureMap", FrameTicks -> {ticks2, None, None, MapAt[Rotate[#, 90 Degree] &, ticks2, {All, 2}]}, PlotLegends -> Automatic, RotateLabel -> True, PlotLabel -> Style[seqgroupname2, FontSize -> 18]]? $\endgroup$ – kglr Apr 24 '20 at 14:25
  • $\begingroup$ An error that seems to amount to a problem with the datatype of the values in my array. The first few are MatrixPlot[{{ComplexInfinity, Indeterminate, ComplexInfinity, Indeterminate, ComplexInfinity, Indeterminate, ComplexInfinity, ComplexInfinity, ComplexInfinity, ComplexInfinity, Indeterminate, ComplexInfinity}, {Indeterminate, ComplexInfinity, Indeterminate, Indeterminate, ComplexInfinity......... $\endgroup$ – Jamie Apr 24 '20 at 14:37
  • $\begingroup$ In a worst case scenario you can try this Show[MatrixPlot[outtabletoprint2, ColorFunction -> "TemperatureMap", FrameTicks -> {ticks2, None, None, MapAt[Rotate[#, 90 Degree] &, ticks2, {All, 2}]}, PlotLegends -> Automatic, RotateLabel -> True, PlotLabel -> Style[seqgroupname2, FontSize -> 18]], MatrixPlot[outtabletoprint2, ColorRules -> {0 -> Black}, ColorFunction -> "TemperatureMap"]] $\endgroup$ – OkkesDulgerci Apr 24 '20 at 14:39
  • 1
    $\begingroup$ Better use string for indexnames2, i.e., indexnames2={"A",...} $\endgroup$ – OkkesDulgerci Apr 24 '20 at 14:43
  • $\begingroup$ I figured it out. A straight Ctrl-C copy of @kglr Option #2 from one notebook to another results in the error. However a Copy As-> Input text eliminates the error. $\endgroup$ – Jamie Apr 24 '20 at 14:46
9
$\begingroup$
SeedRandom[1]
outtabletoprint = RandomInteger[{0, 5}, {20, 20}];

1. You can replace 0s in the input matrix with Black:

MatrixPlot[outtabletoprint /. 0 -> Black, 
 ColorFunction -> "TemperatureMap", PlotLegends -> Automatic]

enter image description here

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).

$\endgroup$
4
  • $\begingroup$ Option #1 did not work. Option #2 works great except no legend is produced. Do you have any idea why this would be. $\endgroup$ – Jamie Apr 24 '20 at 13:29
  • 1
    $\begingroup$ @Jamie, both methods work in version 9.0.1, in version 11.3 (Windows 10 -64bit) and in version 12.1 (Wolfram Cloud). Maybe your version/ OS is different. If so please add your version/OS info to your post. $\endgroup$ – kglr Apr 24 '20 at 13:39
  • $\begingroup$ Option #1 works. The problem was with using Ctrl-C to copy code between notebooks. Using Copy As->Input text fixed the error. Thank you @kglr $\endgroup$ – Jamie Apr 24 '20 at 14:47
  • $\begingroup$ @kglr your second solution does not produce Legend with "12.1.0 for Microsoft Windows (64-bit) (March 14, 2020)" $\endgroup$ – OkkesDulgerci Apr 24 '20 at 15:40
4
$\begingroup$
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}}]]

enter image description here

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}}]]

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.