5
$\begingroup$

Given a MatrixPlot inside a Manipulate, say some $M(t)$ where $M_{ij}$ real for all entries, is there a way to show the values of each $M_{ij}$ inside matrix plot whilst the Manipulate runs?

$\endgroup$
3
  • $\begingroup$ Sorry all I might have been unclear, what I mean is, say, given the 2x2 rotation matrix {{cos a t, sin a t}{-sin a t, cos a t}}, how can one display their values as manipulate varies wrt a? $\endgroup$
    – MKF
    Nov 10, 2018 at 13:45
  • $\begingroup$ This just being an example $\endgroup$
    – MKF
    Nov 10, 2018 at 16:38
  • $\begingroup$ mathematica.stackexchange.com/questions/9884/… solved it! thanks! $\endgroup$
    – MKF
    Nov 10, 2018 at 16:50

2 Answers 2

5
$\begingroup$

Update: Using the example matrix from the comments:

Manipulate[With[{mat = {{Cos[ a t], Sin [a t]}, {-Sin[ a t], Cos[ a t]}}}, 
    MatrixPlot[mat, Mesh -> All, ImageSize -> 1 -> 100, 
      Epilog -> Dynamic[MapIndexed[Text[Style[Round[#, .01], Large, 
       If[Abs[#] >= .5, White, Black]], {#2[[1]], 3 - #2[[2]]} - .5] &, mat, {2}]]]], 
   {{a, 1/2}, 0, 1}, {{t, 0}, -2 Pi, Pi}]

enter image description here

Original answer:

Using a Slider to change the dimensions of the matrix and EventHandler to increase (left mouse click) / decrease (right mouse click) values of matrix elements:

DynamicModule[{m = ConstantArray[0, {20, 20}]},
 Manipulate[mat = m[[;; n, ;; n]]; 
   Dynamic@EventHandler[
     MatrixPlot[mat, Mesh -> All, ImageSize -> 1 -> 50, 
     DataReversed -> True, 
     Epilog -> Dynamic[MapIndexed[Text[Style[#, Large], Reverse@#2 - .5] &, mat, {2}]]], 
  {{"MouseClicked", 1} :> 
     With[{p = Clip[Reverse@Ceiling@MousePosition["Graphics"], {1, n}, {None, None}]},
       If[FreeQ[None]@p, m[[## & @@ p]] += 1]],
   {"MouseClicked", 2} :> 
    With[{p = Clip[Reverse@Ceiling@MousePosition["Graphics"], {1, n}, {None, None}]},
      If[FreeQ[None]@p, m[[## & @@ p]] -= 1]]}], 
  {{n, 5}, 2, 20, 1}]]

enter image description here

enter image description here

$\endgroup$
3
$\begingroup$

Here is using Graphics

SeedRandom@2;
matrix = RandomInteger[10, {5, 5}];
MatrixForm@matrix

$\left(\begin{array}{ccccc} 8 & 4 & 5 & 4 & 7 \\ 4 & 0 & 1 & 0 & 4 \\ 3 & 7 & 3 & 0 & 2 \\ 7 & 8 & 7 & 9 & 3 \\ 6 & 2 & 3 & 8 & 9 \\ \end{array}\right)$

data = Join @@ Reverse@Table[{i, j}, {j, 5}, {i, 1, 5}];
values = Flatten@matrix;
minMax = MinMax@values;
data2 = Flatten /@ Partition[Riffle[values, data], 2];

Legended[Graphics[{EdgeForm[{Black}], 
     ColorData["DarkRainbow"][Rescale[First[#], minMax]], 
     Rectangle[Rest[#]], Black, 
     Text[First[#], Rest[#] + {0.5, 0.5}]} & /@ data2], 
 BarLegend[{"DarkRainbow", minMax}]]

enter image description here

If you want to use MatrixPlot

Overlay[{MatrixPlot[matrix, PlotRangePadding -> None, 
   ImagePadding -> None], 
  Graphics[{Opacity@0, Rectangle[Rest[#]], Opacity@1, Black, 
      Text[First[#], Rest[#] + {0.5, 0.5}]} & /@ data2, 
   PlotRangePadding -> None]}]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Please don't use JPG for non-photographic images! :) $\endgroup$ Nov 10, 2018 at 11:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.