# Plot values of matrixplot as well as the matrixplot itself inside Manipulate [duplicate]

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?

• 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?
– MKF
Nov 10, 2018 at 13:45
• This just being an example
– MKF
Nov 10, 2018 at 16:38
• mathematica.stackexchange.com/questions/9884/… solved it! thanks!
– MKF
Nov 10, 2018 at 16:50

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


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


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


If you want to use MatrixPlot

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