MATLAB: Hello, I was wondering why the last figure in this code keeps giving me a 3D graph instead of a 2D

plotting

clear all, close all, clc
filename = 'PCA1.xlsx';
a = xlsread(filename,"CAP",'B:B')
b = xlsread(filename,"CAP",'C:C')
X = bsxfun(@minus,a,mean(a))
Y = bsxfun(@minus,b,mean(b))
Z = [X,Y]
[U,S,V] = svd(Z,"econ")
figure
subplot(1,2,1)
semilogx(diag(S),'k-o','LineWidth',2.5)
set(gca,'FontSize',15), axis tight, grid on
subplot(1,2,2)
plot(cumsum(diag(S))./sum(diag(S)),'k-o','LineWidth',2.5)
set(gca,'FontSize',15), axis tight, grid on
set(gcf,'Position',[1400 100 3*600 3*250])
figure, hold on
for i=1:size(Z,1)
x = V(:,1)'*Z(i,:)';
y = V(:,2)'*Z(i,:)';
plot(x,y,'r-o','LineWidth',3)
xlabel('PC1')
ylabel('PC2')
view(85,25), grid on, set(gca,'FontSize',15)
set(gcf,'Position',[1400 100 1200 1600])
end

Best Answer

  • Because your view command is changing the camera angle, resulting in a 3D view.
    view(85,25)