一、基本绘图
Plot
plot(X,Y) 创建 Y 中数据对 X 中对应值的二维线图。
plot(Y) 绘制 Y 对一组隐式 x 坐标的图。自动为y配置x坐标
如果 Y 是向量,则 x 坐标范围从 1 到 length(Y)。
如果 Y 是矩阵,则对于 Y 中的每个列,图中包含一个对应的行。x 坐标的范围是从 1 到 Y 的行数。
>> x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
plot(X,Y, LineSpec) 使用指定的线型、标记和颜色创建绘图。
例 ‘--or’是带有圆形标记的红色虚线
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y,'--or')
plot(X1,Y1,...,Xn,Yn) 在同一组坐标轴上绘制多对 x 和 y 坐标。此语法可替代将坐标指定为矩阵的形式。
plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn) 设置每个线条的线型、标记符号和颜色.
%指定线型 颜色 标记
x = 0:pi/10:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
figure
plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')
使用 hold on 保持上一次画图,使用 hold off 来关闭此功能
LineWidth | 线条宽度0.5 (默认) | 正值 |
LineStyle | 线型"-" (默认) | "--" | ":" | "-." | "none" |
Marker | 标记符号"none" (默认) | "o" | "+" | "*" | "." | ... |
添加图例、标签
1)legend()添加图例
图例位置
2)添加标题和轴标签
title、xlabel 和 ylabel 函数为图形添加标题和轴标签。
title():为某个图添加标题
suptitle() 在一个 figure 中画多个子图时,添加一个大标题
x = linspace(0,10,150);%使用linspace 函数将 x 定义为 0 到 10 之间 150 个值组成的向量。
y = cos(5*x);
figure
plot(x,y,'Color',[0,0.7,0.9])
title('2-D Line Plot')
xlabel('x')
ylabel('cos(5x)')
练习1
t =linspace(1,2);
f=t.^2;
g=sin(2*pi*t);
plot(t,f,'b',t,g,'or');
legend({'t^{2}','sin(2\pi t)'},'location','northwest');
title('Mini Assignment #1');
xlabel('Time (ms)');
ylabel('f(t)');
二、 更改图像属性
三、使用Figure画多张图
subplot(m,n,p)
subplot(2,2,[3,4]):连接 3、4 两个位置画一个长图
axis设定不同
grid on/off:使网格可见或不可见
box on/off:使盒子可见或不可见(即数轴右 上方的边框)
axis on/off:使数轴可见或不可见
自定义图片位置
pos1 = [0.1 0.3 0.3 0.3];
subplot('Position',pos1)
y = magic(4);
plot(y)
title('First Subplot')
pos2 = [0.5 0.15 0.4 0.7];
subplot('Position',pos2)
bar(y)
title('Second Subplot')