Matlab簡單教程:繪圖

繪製sin曲線

x = 0:pi/10:2*pi;
y = sin(x);
plot(x,y);

設置樣式

  • 連續線、點線等
x = 0:pi/10:2*pi;
y = sin(x);
plot(x,y,'LineStyle','-.'); % 斷點線

其它可選值:https://cn.mathworks.com/help/matlab/ref/plot.html?s_tid=gn_loc_drop#input_argument_namevalue_d119e766669

  • 線的粗細
x = 0:pi/10:2*pi;
y = sin(x);
plot(x,y,'LineWidth',2);  %線寬爲2,默認爲0.5

其它可選值:https://cn.mathworks.com/help/matlab/ref/plot.html?s_tid=gn_loc_drop#input_argument_namevalue_d119e766777

  • 線的顏色
x = 0:pi/10:2*pi;
y = sin(x);
plot(x,y,'Color','r');   %紅色線

其它可選值:https://cn.mathworks.com/help/matlab/ref/plot.html?s_tid=gn_loc_drop#input_argument_namevalue_d119e766499

  • 數據點樣式
x = 0:pi/10:2*pi;
y = sin(x);
plot(x,y,'Marker','s');   %方塊點

其它可選值:https://cn.mathworks.com/help/matlab/ref/plot.html?s_tid=gn_loc_drop#input_argument_namevalue_d119e766805

設置標題、xlabel、ylabel

x = 0:pi/10:2*pi;
y = sin(x);
plot(x,y);
title('sin demo');
xlabel('x axis');
ylabel('y axis');

在一張圖上繪製多條曲線

繪製兩條曲線

x = 0:pi/10:2*pi;
y1 = sin(x);
y2 = cos(x);

figure
plot(x,y1);
hold on
plot(x,y2)
legend('sin','cos')
hold off

一次繪製一個點

x = 0:pi/10:2*pi;
y = sin(x);

figure
for i=1:length(x)
    plot(x(i),y(i),'Marker', 's')
    hold on
end
hold off

subplot

x = 0:pi/10:2*pi;
y1 = sin(x);
y2 = cos(y);
y3 = x;
y4 = x.^2;
y5 = 1./(x+1);
y6 = exp(x);

figure
subplot(2,3,1) %2x3=6張圖,編號從1到6,從上到下,從左到右
plot(x,y1)
subplot(2,3,2)
plot(x,y2)
subplot(2,3,3)
plot(x,y3)
subplot(2,3,4)
plot(x,y4)
subplot(2,3,5)
plot(x,y5)
subplot(2,3,6)
plot(x,y6)

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