MATLAB繪圖及例子總結


MATLAB繪圖及例子總結

二維圖

例 1

X1=[1,2,4,6,7,8,10,11,12,14,16,17,18,20];
Y1=[1,2,4,6,7,8,10,10,8,7,6,4,2,1];
figure(1)
plot(X1,Y1,'o','MarkerSize',15)
X2=1:20;
Y2=log(X2);
figure(2)
plot(X2,Y2,'o','MarkerSize',15)

結果:

例 2

X1=(0:12)*pi/6;
Y1=cos(3*X1);
X2=(0:360)*pi/180;
Y2=cos(3*X2);
figure(1)
subplot(2,2,1);
plot(X1,Y1,'o','MarkerSize',3); % 設置標准尺寸為3
xlim([0 2*pi]) % 補充知識點xlim
% x軸上下限設定xlim([a,b]);
% y軸上下限設定ylim([a,b])
subplot(2,2,2);
plot(X1,Y1,'LineWidth',2); % 設置線寬度為2
xlim([0 2*pi])
subplot(2,2,3);
plot(X2,Y2,'o','MarkerSize',3);
xlim([0 2*pi])
subplot(2,2,4);
plot(X2,Y2,'LineWidth',2);
xlim([0 2*pi])

結果:

例 3

x=-pi/2:0.01:pi/2;
y=x+sin(x)+exp(x);
plot(x,y,'-ro'); % 顏色紅色
grid on % 網格
title('y的函數圖像');
xlabel('x');
ylabel('y');
legend('y=x+sinx+e^(x)');

結果:

例 4

A=magic(20);
A(9:20,:)=[];
figure(1);
plot(A);
for i=1:5
    for j=1:6
        B(i,j)=i+j;
    end
end
x=0.2:0.2:1;
figure(2)
subplot(2,2,1);plot(B,x,'LineWidth',1.5);
subplot(2,2,2);plot(x,B,'LineWidth',1.5);
C=reshape(1:30,5,6); % 變換成特定維數5×6的矩陣
subplot(2,2,3);plot(B,C,'LineWidth',1.5);
subplot(2,2,4);plot(C,B,'LineWidth',1.5);

結果:

例 5

x=-pi:pi/10:pi;
y=tan(sin(x))-sin(tan(x));
plot(x,y,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10);
% MarkerFaceColor:點符號填充顏色
% MarkerEdgeColor:點邊框顏色

結果:

例 6

clear all
x=-pi:pi/10:pi;
y=[sin(x);sin(x+3);sin(x+5)];
z=[cos(x);cos(x+3);cos(x+5)];
figure;
plot(x,y,'r:*',x,z,'g-.v');
% r是紅色,:是點線,*是星號標記
% g是綠色,-.是點畫線,v是倒三角形

結果:

例 7

% 繪制雙坐標軸
figure
angl=0:0.01*pi:2*pi;
ampl=sin(0:0.01*pi:2*pi);
z=ampl.*(cos(angl)+sqrt(-1)*sin(angl));
[AX,H1,H2]=plotyy(0:200,abs(z),0:200,angle(z)*180/pi);
% angle用來求復數矩陣相位角的弧度值,其取值為-pi到pi,z是一個復數,abs(z)是復數的模
set(get(AX(1),'Ylabel'),'String','amplitude') % amplitude振幅
set(get(AX(2),'Ylabel'),'String','phase')   % phase階段
% get返回某些對象屬性的當前值
set(H1,'LineWidth',3);
set(H2,'LineStyle',':','LineWidth',3)

結果:

例 8

% x軸對數坐標
x=10.^(0.1:0.1:4);
y=1./(x+1000);
figure
subplot(1,2,1);
semilogx(x,y,'+','MarkerSize',5,'LineWidth',2);
title('y=(x+1000)^{-1}')
subplot(1,2,2);
plot(x,y,'+','MarkerSize',5,'LineWidth',2);
title('y=(x+1000)^{-1}')
% y軸對數坐標同理

結果1:

% x軸和y軸均為對數坐標
a=0.1:0.1:5;
x=log10(a);
y=10.^a;
figure
subplot(1,2,1)
loglog(x,y,'+','MarkerSize',5,'LineWidth',2)
title('lgy=10^x')
subplot(1,2,2)
plot(x,y,'+','MarkerSize',5,'LineWidth',2)
title('lgy=10^x')

結果2:

例 9

y=[75.995,91.972,105.711,123.203,131.669...
150.697,179.323,203.212,226.505,249.633,281.422];
figure;
bar(y);

結果:

例 10

x=-2:2;
Y=[6,8,7,4,5;4,8,1,12,0;4,6,21,1,3];
subplot(1,2,1),bar(x',Y','stacked')
xlabel('x'),ylabel('\Sigma y'),colormap(cool)
legend('因素 1','因素 2','因素 3')
subplot(1,2,2),barh(x',Y','grouped') % barh創建水平直方圖
xlabel('y'),ylabel('x')

結果:

例 11

x=[1,5,0.5,3.5,2];
explode=[0,1,0,0,0];
pie(x,explode) % 餅圖
colormap jet
figure
pie3(x,explode)
colormap hsv

結果:

例 12

x=-4:0.1:4;
y=randn(5000,1);
hist(y,x) % 直方圖

結果:

例 13

clear
figure;
x=[1,5,6,7,9,5,1,3,12,20];
y=[20,15,6,3,1,5,3,0,1,5];
subplot(121);
scatter(x,y); % 散點圖
subplot(122);
scatter(x,y,[],[1,0,0],'fill');

結果:

三維圖

例 1

theta=0:0.01*pi:2*pi;
x=sin(theta);
y=cos(theta);
z=cos(4*theta);
figure
plot3(x,y,z,'LineWidth',2);hold on;
theta=0:0.02*pi:2*pi;
x=sin(theta);
y=cos(theta);
z=cos(4*theta);
plot3(x,y,z,'rd','MarkerSize',10,'LineWidth',2)

結果:

例 2

X=-10:0.1:10;
Y=-10:0.1:10;
[X,Y]=meshgrid(X,Y);
Z=-X.^2-Y.^2+200;
mesh(X,Y,Z)

結果:

例 3

figure
surf(X,Y,Z,'FaceColor','red','EdgeColor','none');
camlight left; % 左側加一個發光物體
lighting phong % 光照模式,使圖表面光滑細膩,色彩豐富
view(-15,65) % 視角的角度

結果:

例 4

···
[X,Y,Z]=peaks(30); %peaks函數是一個典型的多元函數,本質上是一個二元高斯分布的概率密度函數
subplot(1,2,1);surfl(X,Y,Z),colormap(copper),title('Default Lighting'),shading interp
subplot(1,2,2);surfl(X,Y,Z,[-90,30],[.55,.6,2,10]),shading interp
···
結果:

例 5

% 等高線圖
[X,Y,Z]=peaks(30); 
figure
subplot(2,2,1);contour(X,Y,Z);axis square
subplot(2,2,2);contour(X,Y,Z,10);axis square
subplot(2,2,3);contour(X,Y,Z,-10:1:10);axis square
subplot(2,2,4);contour(X,Y,Z,':');axis square

結果:

例 6

[X,Y,Z]=peaks(30);
R=sqrt(X.^2+Y.^2);
subplot(1,2,1);
surf(X,Y,Z,Z);
axis tight
subplot(1,2,2);surf(X,Y,Z,R);
axis tight

結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM