MATLAB 繪制3D圖形一些常用的命令
shading:是用來處理色彩效果的,分以下三種:
1、no shading 一般的默認模式 即shading faceted
2、shading flat 在faceted的基礎上去掉圖上的網格線
3、shading interp 在flat的基礎上進行色彩的插值處理,使色彩平滑過渡
clc;clear;close all;
%% 3D lines t = linspace(0,6*pi,30); x = 5*cos(t); y = 4*sin(t); z = 0.02*t.^2; figure hold on plot3(x,y,z,'b','linewidth',2) plot3(x,y,z,'ro','Markersize',16) xlabel('x') ylabel('y') zlabel('z') grid on axis('equal') view([35,30])

figure
scatter3(x,y,z)
xlabel('x') ylabel('y') zlabel('z') grid on axis('equal') view([35,30])

%% 3D Surfaces
x = [1 2 5] y = [2 3 4]; z = [1 3 0]; figure patch(x,y,z,'m')

%mesh
x1 = linspace(-pi,pi,20); x2 = linspace(-10,18,30); [X1,X2] = meshgrid(x1,x2); figure Z = cos(X1).*X2; mesh(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“mesh”命令繪圖') view([35,30]) % axis('equal')

% surf
figure
Z = cos(X1).*X2; surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('Z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) % axis('equal')

figure
x1 = linspace(-pi,pi,200); %數據取得更多 x2 = linspace(-10,18,300); [X1,X2] = meshgrid(x1,x2); Z = cos(X1).*X2; surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) % axis('equal')

figure
x1 = linspace(-pi,pi,200); %數據取得更多 x2 = linspace(-10,18,300); [X1,X2] = meshgrid(x1,x2); Z = cos(X1).*X2; surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) shading interp %去掉繪圖中的線條

figure
x1 = linspace(-pi,pi,20); x2 = linspace(-10,18,30); [X1,X2] = meshgrid(x1,x2); Z = cos(X1).*X2; surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) shading interp %去掉繪圖中的線條 colorbar %給繪圖增加圖例
figure
surf(X1,X2,Z)
xlabel('x_1')
ylabel('x_2')
zlabel('z = f(x_1,x_2)')
grid on
title('使用“surf”命令繪圖')
view([35,30])
shading interp %去掉繪圖中的線條
colormap(jet) % colormap winter/winter/autumn/spring
%colormap map 或者colormap(map)都可以
colorbar %給繪圖增加圖例

figure
surf(X1,X2,Z)
xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) shading interp %去掉繪圖中的網格線條 colormap(jet(5)) % colormap winter/winter/autumn/spring/hsv/hot/cool/gray colorbar %給繪圖增加圖例

%contour
figure
contour(X1,X2,Z)
xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“contour”命令繪圖')

%surfc
figure
surfc(X1,X2,Z)
xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surfc”命令繪圖') view([35,30]) shading interp %去掉繪圖中的線條 colormap(jet) % colormap winter/winter/autumn/spring/hsv/hot/cool/gray colorbar %給繪圖增加圖例

%畫一個復合圖
x1_line = linspace(-1,1,20); x2_line = linspace(-10,10,20); z_line = cos(x1_line).*x2_line; figure hold on surf(X1,X2,Z) plot3(x1_line,x2_line,z_line,'m','linewidth',2) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('復合圖') view([35,30]) shading interp %去掉繪圖中的線條 colormap(jet) % colormap winter/winter/autumn/spring/hsv/hot/cool/gray colorbar %給繪圖增加圖例

% 畫matlab logo
figure
L = 160*membrane(1,100); %使用指令membrane來查看matlab的logo網格圖 surf(L) shading interp colormap autumn
全部代碼:

clc;clear;close all; %% 3D lines t = linspace(0,6*pi,30); x = 5*cos(t); y = 4*sin(t); z = 0.02*t.^2; figure hold on plot3(x,y,z,'b','linewidth',2) plot3(x,y,z,'ro','Markersize',16) xlabel('x') ylabel('y') zlabel('z') grid on axis('equal') view([35,30]) figure scatter3(x,y,z) xlabel('x') ylabel('y') zlabel('z') grid on axis('equal') view([35,30]) %% 3D Surfaces x = [1 2 5]; y = [2 3 4]; z = [1 3 0]; figure patch(x,y,z,'m') %mesh x1 = linspace(-pi,pi,20); x2 = linspace(-10,18,30); [X1,X2] = meshgrid(x1,x2); figure Z = cos(X1).*X2; mesh(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“mesh”命令繪圖') view([35,30]) % axis('equal') % surf figure Z = cos(X1).*X2; surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('Z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) % axis('equal') figure x1 = linspace(-pi,pi,200); %數據取得更多 x2 = linspace(-10,18,300); [X1,X2] = meshgrid(x1,x2); Z = cos(X1).*X2; surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) % axis('equal') figure x1 = linspace(-pi,pi,200); %數據取得更多 x2 = linspace(-10,18,300); [X1,X2] = meshgrid(x1,x2); Z = cos(X1).*X2; surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) shading interp %去掉繪圖中的線條 figure x1 = linspace(-pi,pi,20); x2 = linspace(-10,18,30); [X1,X2] = meshgrid(x1,x2); Z = cos(X1).*X2; surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) shading interp %去掉繪圖中的線條 colorbar %給繪圖增加圖例 figure surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) shading interp %去掉繪圖中的線條 colormap(jet) % colormap winter/winter/autumn/spring %colormap map 或者colormap(map)都可以 colorbar %給繪圖增加圖例 figure surf(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surf”命令繪圖') view([35,30]) shading interp %去掉繪圖中的網格線條 colormap(jet(5)) % colormap winter/winter/autumn/spring/hsv/hot/cool/gray colorbar %給繪圖增加圖例 %contour figure contour(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“contour”命令繪圖') %surfc figure surfc(X1,X2,Z) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('使用“surfc”命令繪圖') view([35,30]) shading interp %去掉繪圖中的線條 colormap(jet) % colormap winter/winter/autumn/spring/hsv/hot/cool/gray colorbar %給繪圖增加圖例 %畫一個復合圖 x1_line = linspace(-1,1,20); x2_line = linspace(-10,10,20); z_line = cos(x1_line).*x2_line; figure hold on surf(X1,X2,Z) plot3(x1_line,x2_line,z_line,'m','linewidth',2) xlabel('x_1') ylabel('x_2') zlabel('z = f(x_1,x_2)') grid on title('復合圖') view([35,30]) shading interp %去掉繪圖中的線條 colormap(jet) % colormap winter/winter/autumn/spring/hsv/hot/cool/gray colorbar %給繪圖增加圖例 % 畫matlab logo figure L = 160*membrane(1,100); %使用指令membrane來查看matlab的logo網格圖 surf(L) shading interp colormap autumn
2020-10-04