4
$\begingroup$

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]

enter image description here

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]

enter image description here

$\endgroup$
5
  • $\begingroup$ @Öskå The range of your non-diagonal elements includes 0. Hence the gradient doesn't change appreciably. My non-diagonal elements have different ranges. $\endgroup$ – becko Dec 5 '14 at 17:11
  • 1
    $\begingroup$ @becko, it would help if you supplied an example matrix to demonstrate the problem $\endgroup$ – Simon Woods Dec 5 '14 at 17:12
  • $\begingroup$ @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. $\endgroup$ – becko Dec 5 '14 at 17:14
  • $\begingroup$ @SimonWoods See edit. Added an example. $\endgroup$ – becko Dec 5 '14 at 17:20
  • $\begingroup$ MatrixPlot[SparseArray[{{i_,i_}:> Min@yourArray,{i_,j_}:>yourArray[[i,j]]},Dimensions@yourArray]] $\endgroup$ – Dr. belisarius Dec 5 '14 at 17:23
5
$\begingroup$
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}]}]

enter image description here

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

enter image description here

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]

enter image description here

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

enter image description here

$\endgroup$
1
$\begingroup$

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

enter image description here

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ – becko Dec 5 '14 at 19:13
0
$\begingroup$

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

enter image description here

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

$\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.