1
$\begingroup$

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]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ 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}]] $\endgroup$
    – Domen
    Commented Sep 3, 2021 at 15:24

2 Answers 2

3
$\begingroup$
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},
    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 -> StringForm["`` = (``  ``) `` ``\n",
      HoldForm[f[x, y]],
      Style["x", Italic],
      Style["y", Italic],
      Hdisp,
      MatrixForm[{x, y}]]]);

rotatedAxes[Pi/4]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks, StringForm seems like a cool thing to know about $\endgroup$ Commented Sep 3, 2021 at 15:57
0
$\begingroup$
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@
     Style[TraditionalForm[Defer@f[x, y]], 16, Red, 
      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.

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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