四个不同视角的相同子图

Four identical subplots with different view angle

我做了如下3D图:

figure
subplot(2,1,1)
hold on
plot3(X_LE, Y_LE,Z, 'red', 'linewidth', 2)
plot3(X_TE, Y_LE,Z, 'red', 'linewidth', 2)
plot3(X_LE, -Y_LE,Z, 'red', 'linewidth', 2)
plot3(X_TE, -Y_LE,Z, 'red', 'linewidth', 2)
plot3([X_LE(end) X_TE(end)],[Y_LE(end) Y_LE(end)],[0 0], 'red', 'linewidth', 2)
plot3([X_LE(end) X_TE(end)],[-Y_LE(end) -Y_LE(end)],[0 0], 'red', 'linewidth', 2)
grid on
axis equal
xlabel('x/b','Interpreter','latex')
ylabel('y/b','Interpreter','latex')
view(-45, 23);

但是,我想创建一个 2x2 子图,其中 4 个子图中的每一个的视角都不同。

与其将整个代码复制 4 次并仅更改视角,是否有一些优雅的方法可以做到这一点?

我试图获得的输出示例:

您可以使用 copyobj 函数。

copyobj 将允许您复制您已经定义的任何图形对象。所以原则是创建你的第一个子图,然后简单地复制它3次并调整每个新副本的位置和视图。

要使用此功能(以及许多其他原因),最好保存您创建的图形对象的句柄。这通常是通过将图形指令的 return 值分配给变量来完成的。例如:

hp = plot(x,y) ;

会将 plot 对象的句柄保留在变量 hp 中,因此您始终可以使用此句柄来修改线属性。

对于你的具体情况,它会是这样的:

%% Quick mock up of a 3D triangle (you did not give any sample data)
x = [0 ;  2 ; 1 ; 0 ] ;
y = [3 ;  1 ; 5 ; 3 ] ;
z = [2 ; -1 ; 4 ; 2 ] ;

%% use dummy subplots, just to save their position on a figure
hf = figure ;
for k=1:4
    hs = subplot(2,2,k) ;
    axpos{k,1} = hs.OuterPosition ;
end
clf(hf) ; % clear all subplots, keep only "axpos" and the empty figure

%% Generate the first subplot
%% (use your own code for that, but don't forget to retrieve the handles of the figure and the axes)
figure(hf) ;
% hs(1) = subplot(2,2,1) ; % use the line below instead. It is equivalent
                           % and it also set the 'hold on' mode for the axe
hs(1) = axes('parent',hf, 'OuterPosition',axpos{1},'NextPlot','add') ;
hp = plot3(x,y,z,'red', 'linewidth', 2) ;
grid on
axis equal
xlabel('x/b','Interpreter','latex')
ylabel('y/b','Interpreter','latex')
view(-45, 23);

%% Now use "copyobj" to copy the full axes object with the content and labels
for k=2:4
    hs(k) = copyobj( hs(1) , hf ) ;          % create a copy of the full subplot
    set( hs(k) , 'OuterPosition',axpos{k} )  % reposition it so it doesn't overlap the original
end

然后你所要做的就是根据你的需要改变每个子图的视图。 这可以通过使用子图句柄作为 view 指令的第一个参数来完成。例如:

%% adjust the view of each subplot
view( hs(2) ,  25,40)
view( hs(3) , -25,32)
view( hs(4) ,  37,92)

注意:如果你事先知道你想要的视图,你也可以将值放在数组的开头,并在循环中直接设置每个轴视图调整位置。

是的,一个优雅的解决方案是从您的代码中创建一个函数,就像这样。

function [y] = changeViewAngle(pos, azimuth, elevation)
X_LE = -1:0.01:1;
X_TE = -1:0.01:1;
Y_LE = -1:0.01:1;
Z = -1:0.01:1;
subplot(2,2,pos)
hold on
plot3(X_LE, Y_LE,Z, 'red', 'linewidth', 2)
plot3(X_TE, Y_LE,Z, 'red', 'linewidth', 2)
plot3(X_LE, -Y_LE,Z, 'red', 'linewidth', 2)
plot3(X_TE, -Y_LE,Z, 'red', 'linewidth', 2)
plot3([X_LE(end) X_TE(end)],[Y_LE(end) Y_LE(end)],[0 0], 'red', 'linewidth', 2)
plot3([X_LE(end) X_TE(end)],[-Y_LE(end) -Y_LE(end)],[0 0], 'red', 'linewidth', 2)
grid on
axis equal
xlabel('x/b','Interpreter','latex')
ylabel('y/b','Interpreter','latex')
view(azimuth, elevation)
end

然后保存为同名文件,即changeViewAngle.m

现在创建另一个脚本,main.m,如下所示,

figure(2);
clear;
clc;
clf;

changeViewAngle(1, -45, 23)
changeViewAngle(2, 45, -23)
changeViewAngle(3, 25, 90)
changeViewAngle(4, 35, 75)

注意:请记住将目录更改为保存这两个文件的目录。如果将它们保存在同一个文件夹中会很方便。否则,MATLAB 可能会抱怨找不到函数。

当然,您还必须根据要制作的情节更改 Z 的值,X_LE、X_TE 和 Y_LE。我没有这些值,所以我在这个函数中使用了一些虚拟值。但我猜你明白如何用 4 个不同的视角绘制 4 个子图,因为那是你问题的重点。