创建2D线图Matlab的3D效果

时间:2018-12-14 15:43:27

标签: matlab matlab-figure

问题,我有一行数据。但是,我希望以3D效果进行绘制,例如,您也可以在Office Excel / Powerpoint中选择该效果。

目前,我在下面的代码中可以看到哪种模仿效果(只需将很多图相互叠加即可):

Excel:

enter image description here

Matlab:

enter image description here

Matlab代码:

x = 1:10;
y = rand(1, 10);
z = rand(1, 10);

figure; 
hold on;
for i = 1:20
   hFill = fill3(i*0.01*ones(1, 12), x([1 1:end end]), [0 y 0], 'b', 'FaceAlpha', 0.5);
   hFill = fill3((i*0.01*(ones(1, 12)))+2, x([1 1:end end]), [0 z 0], 'g', 'FaceAlpha', 0.5);
end

grid on;
xlim([0 10]);
view(3);

1 个答案:

答案 0 :(得分:2)

您可能可以在一个图形对象中构造一个闭合曲面来表示您的3D曲线,但是我没有时间完全解决这个问题,所以我会偷偷摸摸地走: 我没有堆叠多个fill对象来提供3D感觉,而是使用3个对象构建了每个曲线:

  • 具有期望的thickness
  • 的顶表面
  • 2个贴片对象以闭合侧面

在这里:

%% Sample data
rng(12)
x = 1:10;
y = rand(1, 10) + .5 ;
z = rand(1, 10) + .5 ;

%% Parameters
alphaTop  = .5 ; % alpha value of the top of the surface
alphaSide = .5 ; % alpha value of the sides
thickness = .5 ; % thickness of each curve
separation = 1 ;  % separation of each curve
colors = {'b';'r'} ; % color for each curve

%% prepare patch coordinates
xp = x([1 1:end end 1]) ;
yp1 = [0 y 0 0] ;
yp2 = [0 z 0 0] ;
zp1 = zeros(size(yp1)) ;
zp2 = zeros(size(yp2)) + 1 ;

%% Prepare surface coordinates
xs  = [ xp; xp] ;
ys1 = [yp1;yp1] ;
ys2 = [yp2;yp2] ;
zs1 = zeros(size(xs)) ;
zs1(2,:) = zs1(2,:) + thickness ;
zs2 = zs1 + separation ;

%% Display
figure
hold on
% plot the sides (one patch on each side of each curve)
hp11 = patch(zp1          ,xp,yp1, colors{1} , 'FaceAlpha', alphaSide) ;
hp12 = patch(zp1+thickness,xp,yp1, colors{1} , 'FaceAlpha', alphaSide) ;

hp21 = patch(zp2          ,xp,yp2, colors{2} , 'FaceAlpha', alphaSide) ;
hp22 = patch(zp2+thickness,xp,yp2, colors{2} , 'FaceAlpha', alphaSide) ;

% plot the top surfaces
hs1 = surf(zs1,xs,ys1, 'FaceColor',colors{1},'FaceAlpha',alphaTop) ;
hs2 = surf(zs2,xs,ys2, 'FaceColor',colors{2},'FaceAlpha',alphaTop) ;

% refine plot
xlim([0 10]);ylim([0 10]); view(3);
xlabel('X') ; ylabel('Y') ; zlabel('Z') ;

哪种产量:

3d curves

一旦构建,就可以重新组合图形手柄以对通用属性分配进行分组。例如:

%% Optional (modify common properties in group)
% regroup graphic handles for easy common property assignment
hg1 = [hp11;hp12;hs1] ;
hg2 = [hp21;hp22;hs2] ;
% set properties in group
set(hg1,'EdgeColor',colors{1},'FaceAlpha',0.2) ;
set(hg2,'EdgeColor',colors{2},'FaceAlpha',0.2) ;

为曲线提供漂亮的透明 mesh 样式: enter image description here

最终,如果您打算将此方法应用于许多曲线,则应将其打包在函数中,或者至少使用循环构建曲线。只要每条曲线的参数都在可以索引的数组中(就像我对颜色所做的那样),转换就应该很容易。