gpt4 book ai didi

matlab - 四个不同视角的相同子图

转载 作者:太空宇宙 更新时间:2023-11-03 20:03:26 25 4
gpt4 key购买 nike

我制作了以下 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 次并仅更改视角,是否有一些优雅的方法可以做到这一点?

我试图获得的输出示例:

Figure window with 4 subplots, each showing the same data but from different point of view

最佳答案

您可以使用 copyobj功能。

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

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

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 。这可以通过使用子图句柄作为 view 指令的第一个参数来完成。例如:

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

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

关于matlab - 四个不同视角的相同子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53448946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com