9

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

Yaroslav Bulatov
  • 7,793
  • 1
  • 19
  • 44

2 Answers2

14

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
7

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

bobthechemist
  • 19,693
  • 4
  • 52
  • 138