Matlab學習筆記四:三維繪圖

76.

>> x=[0 0 0];y=[30 60 90];z=[0 0 0];%繪製平面Z=0上的直線
plot3(x,y,z)
hold on
>> grid on
>> x1=[0 0 0 0];y1=[0 12 24 36];z1=[3 3 3 3];%繪製平面Z=3上的直線
plot3(x1,y1,z1)
grid on
hold on
>> x2=[0 0 0 0];y2=[0 10 10 0];z2=[0 0 1 1];%繪製未封閉矩形
plot3(x2,y2,z2)
hold on
>> x2=[0 0 0 0 0];y2=[0 10 10 0 0];z2=[0 0 1 1 0];%繪製封閉矩形
plot3(x2,y2,z2)

77.三維餅狀圖
x=[32 45 11 76 56];
explode=[0 0 1 0 1];%1表示分離出來
pie3(x,explode)

78. plot3繪製三維曲線圖
x=-5:0.4:5;
y=5:-0.4:-5;
z=exp(-0.2*x).*cos(y);
[X,Y]=meshgrid(x,y);
Z=exp(-0.2*X).*cos(Y);
figure
subplot(2,1,1)
plot3(x,y,z,'or',x,y,z)
subplot(2,1,2)
plot3(X,Y,Z)

79.設置視角
subplot(2,2,1)
ezmesh(@peaks);
view(3);
[a,b]=view;title(mat2str([a,b]))        %-37.5 30
subplot(2,2,2)
ezmesh(@peaks);
view(2);
[a,b]=view;title(mat2str([a,b]))          %0 90
subplot(2,2,3)
ezmesh(@peaks);
view([30 45]);
[a,b]=view;title(mat2str([a,b]))          %30 45
subplot(2,2,4)
ezmesh(@peaks);
view([1 1 sqrt(2)]);
[a,b]=view;title(mat2str([a,b]))           %135 45

>> help view
 VIEW   3-D graph viewpoint specification.
    VIEW(AZ,EL) and VIEW([AZ,EL]) set the angle of the view from which an
    observer sees the current 3-D plot.  AZ is the azimuth or horizontal
    rotation and EL is the vertical elevation (both in degrees). Azimuth
    revolves about the z-axis, with positive values indicating counter-
    clockwise rotation of the viewpoint. Positive values of elevation
    correspond to moving above the object; negative values move below.
    VIEW([X Y Z]) sets the view angle in Cartesian coordinates. The
    magnitude of vector X,Y,Z is ignored.
 
    Here are some examples:
 
    AZ = -37.5, EL = 30 is the default 3-D view.
    AZ = 0, EL = 90 is directly overhead and the default 2-D view.
    AZ = EL = 0 looks directly up the first column of the matrix.
    AZ = 180 is behind the matrix.

80.創建多個片塊模型
subplot(1,2,1)
X=[0 0;0 1;0 1;0 0];
Y=[0 0;1 0;1 1;0 1];
Z=[0 0;0 0;1 0;1 0];
patch(X,Y,Z,'r')%填充
view([30,30])
subplot(1,2,2)
Vm=[0 0 0;1 0 0;1 0 1;0 0 1;0 1 1;1 1 1;1 1 0];
Fm=[1 2 3 4;3 4 5 6;6 3 2 7];
patch('Vertices',Vm,'Faces',Fm,'EdgeColor','r')%畫出邊緣
view([30,30])

81.創建片塊模型
subplot(2,2,1)
x=[0 1 0.5];y=[0 0 1];
fill(x,y,'r');title('fill')
subplot(2,2,2)
X=[0 0 0 0];Y=[0 1 1 0];Z=[0 0 1 1];
fill3(X,Y,Z,'g');title('fill3')
subplot(2,2,3)
patch(X,Y,Z,'b');view([30,30])
title('patch:high-level syntax')
subplot(2,2,4)
patch('XData',X,'YData',Y,'ZData',Z);view([30,30])
title('patch:low-level syntax')

82.非網格數據點繪圖
x=rand(1,20);
y=rand(1,20);
z=cos(0.5.*x).*sin(y);
xi=linspace(0,1,50);
yi=linspace(0,1,50);
[X,Y]=meshgrid(xi,yi);
subplot(2,2,1)
Z1=griddata(x,y,z,X,Y,'linear');
mesh(X,Y,Z1)
title('linear')
subplot(2,2,2)
Z1=griddata(x,y,z,X,Y,'nearest');
mesh(X,Y,Z1)
title('nearest')
subplot(2,2,3)
Z1=griddata(x,y,z,X,Y,'cubic');
mesh(X,Y,Z1)
title('cubic')
subplot(2,2,4)
Z1=griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z1)
title('v4')

83.簡易三維繪圖函數
subplot(2,2,1)
ezplot3('sin(t)','cos(t)','sin(2*t)',[0,2*pi])
subplot(2,2,2)
ezmesh(@peaks,[-5 5 -5 5])
subplot(2,2,3)
ezsurf(@(x,y)(x.^2+y.^2),[-5 5 -5 5])
subplot(2,2,4)
ezsurfc(@(x,y)(x.^2+y.^2),[-5 5 -5 5])

84.矩形網格
x=-5:0.5:5;
y=5:-0.5:-5;
[X,Y]=meshgrid(x,y);
whos
plot(X,Y,'o')

85.三維表面圖
close all
clear
[X,Y] = meshgrid(-3:.5:3);
Z = 2*X.^2-3*Y.^2;
subplot(2,2,1)
mesh(X,Y,Z)
title('mesh')
subplot(2,2,2)
surf(X,Y,Z)
title('surf')
subplot(2,2,3)
surfc(X,Y,Z)
title('surfc')
subplot(2,2,4)
surfl(X,Y,Z)
title('surfl')

86.三維等值線圖
clear
close all
[X,Y]=meshgrid(-3:0.01:3);
Z=X.^2+Y.^2;
contour3(X,Y,Z,20)
view([45 50])

87.三維火柴桿圖stem3
clear
x=rand(1,10);
y=rand(1,10);
z=x.^2+2*y;
stem3(x,y,z,'fill')

88.三維散點圖
close all
clear
x=rand(1,10);
y=rand(1,10);
z=x.^2+y.^2;
scatter3(x,y,z,'ro')
hold on
[X,Y]=meshgrid(0:0.1:1);
Z=X.^2+Y.^2;
mesh(X,Y,Z)
hidden off

89.三維網線圖
close all
clear
[X,Y] = meshgrid(-3:.5:3);
Z = 2*X.^2-3*Y.^2;
subplot(2,2,1)
plot3(X,Y,Z)
title('plot3')
subplot(2,2,2)
mesh(X,Y,Z)
title('mesh')
subplot(2,2,3)
meshc(X,Y,Z)
title('meshc')
subplot(2,2,4)
meshz(X,Y,Z)
title('meshz')

90.三維向量場圖
clear
close all
[X,Y]=meshgrid(-3:0.4:3);
Z=-3*X.^2-Y.^2;
[U,V,W]=surfnorm(X,Y,Z);
quiver3(X,Y,Z,U,V,W,0.2)
hold on
surf(X,Y,Z)

91.三維柱狀圖
clear
x=rand(3,10);
subplot(2,2,1)
bar(x)
title('bar')
subplot(2,2,2)
barh(x,'stack')
title('barh-stack')
subplot(2,2,3)
bar3(x)
title('bar3')
subplot(2,2,4)
bar3h(x,'stack')
title('bar3h-stack')

92.設置多個片塊模型的顏色
clear
close all
Vm=[0 0 0;1 0 0;1 0 1;0 0 1;0 1 1;1 1 1;1 1 0];
Fm=[1 2 3 4;3 4 5 6;6 3 2 7];
subplot(3,2,1)
patch('Vertices',Vm,'Faces',Fm,'FaceVertexCData',rand(1,1),'FaceColor','flat')
view([30,30]);title('set uniform index-color')
subplot(3,2,2)
patch('Vertices',Vm,'Faces',Fm,'FaceVertexCData',rand(1,3),'FaceColor','flat')
view([30,30]);title('set uniform RGB-color')
subplot(3,2,3)
patch('Vertices',Vm,'Faces',Fm,'FaceVertexCData',rand(size(Vm,1),1),'FaceColor','interp')
view([30,30]);title('assign multi-index-color to Vertices')
subplot(3,2,4)
patch('Vertices',Vm,'Faces',Fm,'FaceVertexCData',rand(size(Vm,1),3),'FaceColor','interp')
view([30,30]);title('assign multi-RGB-color to Vertices')
subplot(3,2,5)
patch('Vertices',Vm,'Faces',Fm,'FaceVertexCData',rand(size(Fm,1),1),'FaceColor','flat')
view([30,30]);title('assign multi-index-color to Faces')
subplot(3,2,6)
patch('Vertices',Vm,'Faces',Fm,'FaceVertexCData',rand(size(Fm,1),3),'FaceColor','flat')
view([30,30]);title('assign multi-RGB-color to Faces')

93.設置座標軸
close all
subplot(1,3,1)
ezsurf(@(t,s)(sin(t).*cos(s)),@(t,s)(sin(t).*sin(s)),@(t,s)cos(t),[0,2*pi,0,2*pi])
axis auto;title('auto')

subplot(1,3,2)
ezsurf(@(t,s)(sin(t).*cos(s)),@(t,s)(sin(t).*sin(s)),@(t,s)cos(t),[0,2*pi,0,2*pi])
axis equal;title('equal')

subplot(1,3,3)
ezsurf(@(t,s)(sin(t).*cos(s)),@(t,s)(sin(t).*sin(s)),@(t,s)cos(t),[0,2*pi,0,2*pi])
axis square;title('square')

94.網格邊框線設置
close all
clear
[X,Y] = meshgrid(-3:.25:3);
Z = -sqrt(X.^2+3*Y.^2);
subplot(1,2,1)
mesh(X,Y,Z)
hidden on
title('hidden on')
subplot(1,2,2)
mesh(X,Y,Z)
hidden off
title('hidden off')

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章