Matlab简单教程:绘图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012314976/article/details/78606410

绘制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)

猜你喜欢

转载自blog.csdn.net/u012314976/article/details/78606410