9
$\begingroup$

What is the right way to visualize a second set of "rotated" coordinate axes on a plot? As an example, for the diagram below, I want to show the choice of coordinate system which makes the distribution centered at 0 with diagonal covariance matrix. How should I draw this? I tried Show[a,Rotate[a]] but get "Could not combine the graphics objects"

sigma = {{1, 0}, {0, 1/10}};
rot = RotationMatrix[Pi/4];
dist = MultinormalDistribution[{2, 2}, rot.sigma.rot\[Transpose]];
a = ListPlot[RandomVariate[dist, 10000], 
  PlotRange -> {{-5, 5}, {-5, 5}}, AspectRatio -> 1]

enter image description here

$\endgroup$
13
$\begingroup$

You can use the new in M12.3 AxisObject to do this. For example:

sigma = {{1, 0}, {0, 1/10}};
rot = RotationMatrix[Pi/4];
dist = MultinormalDistribution[{2,2}, rot . sigma . rot\[Transpose]];
a = ListPlot[
    RandomVariate[dist,10000],
    PlotRange->{{-5,5},{-5,5}},
    AspectRatio->1,
    Epilog->{
        AxisObject[Line[{{0, 0}, {4, 4}}], {-1, 1}],
        AxisObject[Line[{{0, 4}, {4, 0}}], {-1, 1}]
    }
]

enter image description here

$\endgroup$
1
  • $\begingroup$ Woah - I need to upgrade! $\endgroup$ Jul 14 '21 at 11:07
7
$\begingroup$

Here's one manual approach using Overlay. You will need to fine tune the size of the 2nd axis to align more generally.

data = RandomVariate[dist, 10000];
b = ListPlot[data, PlotRange -> {{-5, 5}, {-5, 5}}, AspectRatio -> 1];
Manipulate[
 Overlay[{b, Rotate[ListPlot[{}, AspectRatio -> 1], theta Degree]}, 
  Alignment -> {x, y}],
 {{theta, 0}, 0, 360},
 {{x, 0}, -1, 1},
 {{y, 0}, -1, 1}]

enter image description here

update 1

If the real data are as simple as the sample set, the following code gets the angle right and is close to getting the origin correct.

b = ListPlot[data, PlotRange -> {{-5, 5}, {-5, 5}}, AspectRatio -> 1];
c = ListPlot[{}, AspectRatio -> 1];
Overlay[{b, 
  Rotate[c, VectorAngle[{1, 0}, First@Eigenvectors@Covariance@data]]},
  Alignment -> 
  MapThread[
   Rescale[#1, #2] &, {1, -1} {Mean@data, MinMax /@ Transpose@data}]]

enter image description here

$\endgroup$
2
  • $\begingroup$ thanks for the suggestion, overlay ended up better for me for animated diagram purposes. How did you produce the manipulate animation? $\endgroup$ Aug 3 '21 at 14:12
  • $\begingroup$ I use licecap on Windows. Pretty quick and easy (with little fuss or features). $\endgroup$ Aug 3 '21 at 15:34

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.