Two questions with one answer
Centre the data, so the mean $\bar{\mathbf x}$ sits at the origin, and pick a unit direction $\mathbf v$. Each point $\mathbf x_i$ has a shadow on the line, $(\mathbf v^\top \mathbf x_i)\,\mathbf v$, and a residual from that shadow back to the point. You can ask for either of two things:
- keep the most spread: maximise the variance of the shadows, $\tfrac1n\sum_i (\mathbf v^\top \mathbf x_i)^2$;
- lose the least: minimise the mean squared residual.
By Pythagoras, each point’s squared length is shadow² + residual², and the total does not depend on $\mathbf v$. So the two goals are the same goal. On the angle plot, the orange curve and the dashed curve always add up to the same constant.
Why eigenvectors appear
With the covariance matrix $\Sigma = \tfrac1{n-1}\sum_i \mathbf x_i \mathbf x_i^\top$, the variance along $\mathbf v$ is a quadratic form:
$$\operatorname{Var}(\mathbf v^\top \mathbf x) = \mathbf v^\top \Sigma\, \mathbf v.$$Maximising it subject to $\|\mathbf v\| = 1$ (one Lagrange multiplier) gives $\Sigma \mathbf v = \lambda \mathbf v$. The best direction is the eigenvector with the largest eigenvalue, and that eigenvalue is the variance it captures. In 2-D, writing $\theta_1$ for PC1’s angle, the curve you are dragging along is
$$v(\varphi) = \lambda_1 \cos^2(\varphi - \theta_1) + \lambda_2 \sin^2(\varphi - \theta_1),$$a sinusoid that peaks at PC1 and bottoms out at PC2, 90° away. In $d$ dimensions the first $k$ eigenvectors give the best $k$-dimensional subspace (Eckart–Young). This is why PCA, the SVD and low-rank compression are the same idea.
Surprise 1: units choose the answer
PCA has no idea what your columns mean. Measure x in centimetres instead of metres and its variance grows 10,000×, so PC1 swings round to point along x. It hasn’t found anything new. It is reporting your unit choice. The usual fix is to standardise each feature to variance 1 first, which is PCA on the correlation matrix instead.
Try it: , then tick “standardise”.
Surprise 2: one point can rotate everything
Variance squares distances, so a single far-away point can outweigh dozens of ordinary ones. Drag one point far out along the minor axis and PC1 turns to chase it. Robust PCA variants exist for exactly this reason.
Try it: and drag it around.
Surprise 3: round clouds have no principal direction
When $\lambda_1 \approx \lambda_2$ the angle curve is flat and PC1 is decided by noise: resample and it spins at random. A component is only meaningful when its eigenvalue stands clearly apart from the next.
Try it: , then press Resample a few times.