# How to exclude the diagonal in a MatrixPlot?

I have a matrix where the diagonal elements are meaningless. Usually I just fill them with 0, or infinities. The problem is that when I MatrixPlot this matrix, the diagonal elements affect the gradient scale. I just want to exlucde the diagonal from the plot, leave it blank. How can I do this?

Example:

mat = RandomReal[{100, 102}, {10, 10}];
mat2 = mat - DiagonalMatrix@Diagonal@mat;


Here mat2 is a matrix with zeros on the diagonal. When I plot it with MatrixPlot, I subtract the minimum element to display the variability of the matrix:

MatrixPlot[mat2 - Min@mat2]


Obviously the variability of mat2 is hidden by the zeros on the diagonal. This is clearly visible if we plot the original matrix:

MatrixPlot[mat - Min@mat]


• @Öskå The range of your non-diagonal elements includes 0. Hence the gradient doesn't change appreciably. My non-diagonal elements have different ranges. – becko Dec 5 '14 at 17:11
• @becko, it would help if you supplied an example matrix to demonstrate the problem – Simon Woods Dec 5 '14 at 17:12
• @SimonWoods I just want the diagonal to be drawn white, independently of the color gradient used, and the gradient should not depend on the values at the diagonal. – becko Dec 5 '14 at 17:14
• @SimonWoods See edit. Added an example. – becko Dec 5 '14 at 17:20
• MatrixPlot[SparseArray[{{i_,i_}:> Min@yourArray,{i_,j_}:>yourArray[[i,j]]},Dimensions@yourArray]] – Dr. belisarius Dec 5 '14 at 17:23

t1 = Table[x^2 + y^2, {x, -3, 3}, {y, -3, 3}];
t2 = t1; t2 = ReplacePart[t2, {i_, i_} :> Null];

Row[{MatrixPlot[t1, ColorFunction -> Hue, ImageSize -> 400],
MatrixPlot[t2, ColorFunction -> Hue, ImageSize -> 400, ColorRules -> {Null -> None}],
MatrixPlot[t2 - Min@t1, ColorFunction -> Hue, ImageSize -> 400, ColorRules -> {Null -> None}]}]


Row[{MatrixPlot[t1, ImageSize -> 400],
MatrixPlot[t2, ImageSize -> 400, ColorRules -> {Null -> None}],
MatrixPlot[t2 - Min@t1, ImageSize -> 400, ColorRules -> {Null -> None}]}]


Update:

I need a PlotLegend in my plot

t2b = DeleteDuplicates[Sort[Join @@ t2 /. Null -> (min = Min@t1 - 1), Greater]] /. min -> "Null";
mp = MatrixPlot[t2, ImageSize -> 400, ColorRules -> {Null -> None}];
legend = MatrixPlot[List /@ t2b, ColorRules -> {"Null" -> None},
FrameTicks -> {{None, Transpose[{Range[Length@t2b], t2b}]}, {None, None}},
PlotRangePadding -> 0, ImageSize -> {60, Automatic}];

Legended[mp, legend]


• PlotLegends -> True works with t1, but not with t2. It doesn't seem to understand Null... I need a PlotLegend in my plot :( – becko Dec 5 '14 at 19:06
• @becko, could not get PlotLegends->... to work with ColorRules in version 9. Updated with a way to add legends using Legended. – kglr Dec 5 '14 at 19:43
• Thanks. Let me just point out that I ended up using BarLegend instead of plotting a legend using MatrixPlot. – becko Dec 15 '14 at 21:07
mat = RandomReal[{100, 102}, {10, 10}];
MatrixPlot[(mat - Min@mat) - DiagonalMatrix@Diagonal@(mat - Min@mat)]


Maybe this:

mat = RandomReal[{100, 102}, {10, 10}];
mat2 = mat - DiagonalMatrix@Diagonal@mat;

min = Min[SparseArray[mat2]["NonzeroValues"]]
(* 100.083 *)

MatrixPlot[(mat2 - min) (1 - IdentityMatrix[10])]


• This paints the diagonal elements using the color in the gradient scale corresponding to the minimum matrix element. That's not what I want. I want the diagonal elements to receive a separate color, unrelated to the color used for non-diagonal elements. Otherwise it might give the impression that diagonal elements equal the minimum matrix elements. – becko Dec 5 '14 at 19:13

I'm not sure if I follow exactly what you want to achieve , but you can simply paint over the diagonal with whatever color you wish:

 mat = RandomReal[{100, 102}, {10, 10}];
MatrixPlot[(mat - Min@mat) ,
Epilog -> {Gray,
Table[ Rectangle[#, # + 1] &@{i - 1, Length@mat - i}, {i,
Length@mat}] }]


( maybe do Rectangle[# - .015, # + 1 + .015] to cover a little better.. )