Matlab三維繪圖

%% 1.三維線圖繪製plot3()
%% 2.三維網格指令
%% 3.三維曲面指令
%% 4.色圖
%% 5.三維表面圖形色彩的濃淡處理
%% 6.圖形的鏤空和裁切
%% 7.圖像讀寫
%% 8.播放動畫
%% 9.三維簡易繪圖指令
%% 10.三維直方圖
%% 11.三維扇形圖的繪製
%% 12.離散杆圖






% 1.三維線圖繪製plot3()


t=linspace(0,5*pi,100);
x=t;
y=cos(t);
z=sin(t);
plot3(x,y,z,'g-p');%三維線圖繪製指令,用法同plot()
xlabel('x');
ylabel('y');
zlabel('z');


% 2.三維網格指令


x=linspace(0,4,30);
y=linspace(0,3,20);
[X,Y]=meshgrid(x,y);%創建網格指令,X、Y均爲m x n維,將x、y分別複製給X的行元素,Y的列元素,meshgrid(x)等價於meshgrid(x,x)
                    %meshgrid創建的網格座標X、Y以及由他們計算出來的Z,各列或各行對應於一條空間曲線,空間曲線的集合組成空間畫面                     
Z=X.^2+Y.^2;
mesh(X,Y,Z);%網格繪製指令
xlabel('x');
ylabel('y');
zlabel('z');
title('三維網格函數繪圖');
x=linspace(0,2*pi,200);
y=x;
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
Z=meshc(X,Y,Z);%繪製帶有輪廓線的三維網格圖,同 mesh() 函數功能、用法一樣


% 3.三維曲面指令


x=linspace(-pi,pi,30);
[X,Y]=meshgrid(x);  %創建網格點座標
Z=2*(X+Y)*exp(-(X.^2+Y.^2)/2);
subplot(2,2,1);
surf(X,Y,Z);    %三維曲面圖繪製指令
title('曲面圖');
colormap(autumn);%曲面顏色控制指令
subplot(222);
surfc(X,Y,Z);  %繪圖的時候多了條網格線,用法同surf
title('帶輪廓線的曲面圖');
subplot(223);
mesh(X,Y,Z);
title('曲面圖');
subplot(224);
contour3(X,Y,Z,10);%繪製10條三維等高線
title('三維等高圖');


% 4.色圖


[x,y]=meshgrid(-2:0.1:2,-1:0.1:1);
z=sqrt(x.^2+y.^2);
surf(z);
colormap([0 1 0]);%設置色圖,矩陣的值代表rgb三色的分量
colormap(hot(128));%重新設置色圖
cmap=colormap;%獲取當前色圖矩陣


% 5.三維表面圖形色彩的濃淡處理


x=linspace(-pi,pi,30);
[X,Y]=meshgrid(x);
Z=X.*exp(-(X.^2+Y.^2)/2);
%surf(Z);%此處用於顯示原圖
subplot(1,3,1);
surf(X,Y,Z);
title('shading faceted');%默認顯示方式故省略,立體表現最強
subplot(1,3,2);
surf(X,Y,Z);
shading flat;%濃淡處理,平滑
title('shading flat');
subplot(1,3,3);
surf(X,Y,Z);
shading interp;%濃淡處理,很平滑
title('shading interp');


% 6.圖形的鏤空和裁切


x=linspace(-2,2,20);
[X,Y]=meshgrid(x);
Z=sin(X).*cos(Y);
Z1=Z;
Z2=Z;
Z1(12:16,3:5)=NaN;%給需要縷空部分的數據賦值實現縷空
subplot(1,2,1);
surf(X,Y,Z1);
title('縷空圖');
ii=find(abs(X)>1.5|abs(Y)>1.5);
Z2(ii)=0;     %給需要剪切部分賦值爲零,實現數據個
subplot(122);
surf(X,Y,Z2);


% 7.圖像讀寫


[X,cmap]=imread('F:\\1.jpg');%讀入圖像,第一個參數爲圖像的數據矩陣、第二個參數爲圖像的伴隨顏色
image(X);%顯示圖像
colormap(cmap); %顯示圖像的伴隨顏色
imwrite(X,'E:\\ts.jpg','Quality',100);%存儲圖像
image(imread('ts.jpg'));
axis image off;%隱藏座標系


% 8.播放動畫


[X,Y,Z]=sphere(40);%此函數生成三個(n+1)×(n+1)階的矩陣,再利用函數surf(x,y,z)可生成單位球面,默認值20
h=surf(X,Y,Z);
% colormap(jet);%顏色設置
% shading interp;%下面的是燈光設置
% light,lighting flat;
% light('position',[-1,-1,-2],'color','r');
% light('position',[-1,0.5,1],'style','local','color','g');
axis off %關閉座標軸顯示
for i=1:10
    rotate(h,[0,0,1],30);%圖像繞z軸旋轉,轉速爲30度/s
    mmm(i)=getframe;%獲取圖像數據
end
movie(mmm,8,10);%以每秒10幀速度播放8次


% 9.三維簡易繪圖指令


ezplot3('sin(t)','cos(t)','sin(t)*cos(t)',[0,3*pi]);%繪製x=sin(t)、y=cos(t)、z=sin(t)*cos(t)的圖像,
                                                        % 最後一個參數爲t的取值範圍,默認[0,2pi]
ezsurf('(x.^2-2*x)*exp(-x.^2-y.^2)',[-2,2,-2,2]);%簡易網面圖繪製,-2<x<2,-2<y<2
ezmesh('sin(x)*cos(y)+sin(x)',[-3,3,-3,3]);%簡易繪圖,-3<x<3,-3<y<3,缺省時x、y∈[-2pi,2pi]
ezmesh('sin(t)*cos(s)','cos(2*t)+sin(s)','t-s',[-pi,pi,-pi,pi]);%參數方程式繪圖,-pi<s<pi,-pi<t<pi,缺省時s、t∈[-2pi,2pi]


% 10.三維直方圖


Z=magic(3);
subplot(1,2,1);
bar3(Z,1,'stacked');%繪製 m x n 矩陣z的三維直方圖,向量y默認爲1:m,第二個參數用來指定豎條的寬度(默認0.8)
                    %如果寬度大於1則重合,最後一個參數表示顯示模式,這裏是累加式顯示(還有detached分離式、grouped分組式)
xlabel('x');
ylabel('y');
zlabel('z');
title('豎直三維直方圖');
subplot(122);
bar3h(Z');%默認爲分組式顯示,用來繪製三維水平直方圖
xlabel('x');
ylabel('y');
zlabel('z');
title('水平三維直方圖');


% 11.三維扇形圖的繪製


y=[1 5 7 2 3 6];
explode=[0 0 1 1 0 0];
subplot(1,2,1);
pie(y,explode);%二維扇形圖
legend('a','b','c','d','e','f');
subplot(122);
pie3(y,[0 0 1 1 0 0]);%三維扇形圖


% 12.離散杆圖


t=linspace(0,2*pi,72);
x=sin(t);
y=cos(t);
z=abs(sin(6*t));
stem3(x,y,z,'filled','r');%離散桿狀圖繪製函數
hold on;
plot3(x,y,z,'g-');
hold off;


% 範圍圖解
ezmesh('-sqrt((sqrt(x.^2+y.^2)-1).^2-4)');
hold on;
ezmesh('-sqrt(9-x.^2-y.^2)');
ezmesh('sqrt((sqrt(x.^2+y.^2)-1).^2-4)');
ezmesh('sqrt(9-x.^2-y.^2)');
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章