# Generating PlotLabel with mathematical formatting

What's a good pattern to insert properly formatted matrices into PlotLabel? I'm trying to insert something like the following into my plot, where the matrix is programmatically generated.

$$f(x,y)= \left( \begin{array}{cc} x&y \end{array} \right) \left( \begin{array}{cc} 1.00 & 0.00 \\ 0.00 & 2.00 \\ \end{array} \right) \left( \begin{array}{c} x \\ y \\ \end{array} \right)$$

Doing PlotLabel->MatrixForm[mymatrix] displays the matrix part properly, but how do I concatenate it with the other bits?

rotatedAxes[angle_] := (
H = With[{r = RotationMatrix[Pi/4]},
r . DiagonalMatrix[{1, 2}] . Inverse[r]];
f[{x_, y_}] = {x, y} . H . {x, y};

rotmat = RotationMatrix[angle];
xaxis = rotmat . {5, 0};
yaxis = rotmat . {0, 5};
Hdisp =
Map[NumberForm[#, {3, 2}] &,
N[Inverse[rotmat] . H . rotmat], {2}] // MatrixForm;

ContourPlot[f[{x, y}], {x, -2, 2}, {y, -2, 2},
ContourShading -> None, Contours -> 15,
ContourStyle -> Directive[RGBColor[0.5, 0.5, 1]], Frame -> None,
AxesStyle -> Directive[Black],
Epilog -> {
AxisObject[InfiniteLine[{{0, 0}, xaxis}], {-1, 1},
TickLabels -> None],
AxisObject[InfiniteLine[{{0, 0}, yaxis}], {-1, 1},
TickLabels -> None]},
PlotLabel -> Hdisp
]
);
rotatedAxes[Pi/4]

• Something like this? mat = RandomInteger[5, {2, 2}] // MatrixForm; row = {{x, y}} // MatrixForm; col = {{x}, {y}} // MatrixForm; Plot[x, {x, -3, 3}, PlotLabel -> Row[{HoldForm[f[x, y]], " = ", row , mat, col}]] Sep 3, 2021 at 15:24

Clear["Global*"]

rotatedAxes[angle_] := (H = With[{r = RotationMatrix[Pi/4]},
r . DiagonalMatrix[{1, 2}] . Inverse[r]];
f[{x_, y_}] = {x, y} . H . {x, y};
rotmat = RotationMatrix[angle];
xaxis = rotmat . {5, 0};
yaxis = rotmat . {0, 5};
Hdisp = Map[NumberForm[#, {3, 2}] &,
N[Inverse[rotmat] . H . rotmat], {2}] //
MatrixForm;
ContourPlot[f[{x, y}], {x, -2, 2}, {y, -2, 2},
Contours -> 15,
ContourStyle -> Directive[RGBColor[0.5, 0.5, 1]],
Frame -> None,
AxesStyle -> Directive[Black],
Epilog -> {
AxisObject[InfiniteLine[{{0, 0}, xaxis}], {-1, 1},
TickLabels -> None],
AxisObject[InfiniteLine[{{0, 0}, yaxis}], {-1, 1},
TickLabels -> None]},
PlotLabel -> StringForm[" = (  )  `\n",
HoldForm[f[x, y]],
Style["x", Italic],
Style["y", Italic],
Hdisp,
MatrixForm[{x, y}]]]);

rotatedAxes[Pi/4]

• Thanks, StringForm seems like a cool thing to know about Sep 3, 2021 at 15:57
rotatedAxes[
angle_] := (H =
With[{r = RotationMatrix[Pi/4]},
r . DiagonalMatrix[{1, 2}] . Inverse[r]];
f[{x_, y_}] = {x, y} . H . {x, y};
rotmat = RotationMatrix[angle];
xaxis = rotmat . {5, 0};
yaxis = rotmat . {0, 5};
Hdisp =
Map[NumberForm[#, {3, 2}] &,
N[Inverse[rotmat] . H . rotmat], {2}] // MatrixForm;
pe = Text@
Style[TraditionalForm[" = "], 16, Red, FontFamily -> "Times"];
p2 = Text@
FontFamily -> "Times"];
p3 = Text@
Style[TraditionalForm[f[x, y] = {{x, y}} . Hdisp . {{x}, {y}}],
16, Red, FontFamily -> "Times"];
ContourPlot[f[{x, y}], {x, -2, 2}, {y, -2, 2},
ContourShading -> None, Contours -> 15,
ContourStyle -> Directive[RGBColor[0.5, 0.5, 1]], Frame -> None,
AxesStyle -> Directive[Black],
PlotLabel ->
Apply[StringJoin, ToString[#, StandardForm] & /@ {p2, pe, p3}]
(*Epilog\[Rule]{AxisObject[InfiniteLine[{{0,0},xaxis}],{-1,1},
TickLabels\[Rule]None],AxisObject[InfiniteLine[{{0,0},yaxis}],{-1,
1},TickLabels\[Rule]None]}]*)]);

rotatedAxes[Pi/12]

Some objects on your plot such as AxisObject are not available on my system. The screen shot shows that and the lines are not rendered. But it works still with the plot label and calculation. I think it will work on your setup.

• AxisObject is new on 12.3.1. This works, although seems more complicated than StringForm Sep 3, 2021 at 16:05
• @YaroslavBulatov There is always something to learn.
– Syed
Sep 3, 2021 at 16:27