目录
Modifying Properties of An Object
Several small plots “in a figure”
Control of Grid, Box, and Axis
Basic plotting
plot()
plot(x,y);
例如:
plot(cos(0:pi/20:2*pi));
hold on&hold off
plot(x1,y1);
plot(x2,y2);
%只显示第二张图,matlab绘图时会把旧的图形覆盖
hold on
plot(x1,y1);
plot(x2,y2);
hold off
%使用hold on指令可以将两个图形绘制在一张画布上
%或者
plot(x1,x2,x2,y2);
Plot Style
plot(x,y,'str')
如plot(cos(:pi/20*2*pi),'or--')
legend()——添加图例
当画布中有多个图形时可以用legend()函数添加图例
如:
x=(0:pi/10:pi);
y=sin(x);
z=cos(x);
plot(x,y,'or-',x,z,'ob:');
legend('sinx','cosx');%顺序和plot()中画图的顺序一致
title() and ?label()
- title(' ')
- xlabel(' ')
- ylabel(' ')
- zlabel(' ')
Modifying Properties of An Object
修改一个object的属性首先要先找到这个object的handle。
一共有三种object分别是:
- figure object
- line object
- axes object
其中figure object 和axes object 的handle通过gcf 和gca获取,line object 的handle通过plot()返回
如:h=plot(x,y);
获取一个object的handle之后使用get()函数可以读取这个object的属性,然后找到要修改的属性,使用set函数进行修改。
xlim()&ylim()
xlim()和ylim()两个函数可以用来修改x轴和y轴的范围
如:
xlim([0, 2*pi]);
ylim([-1.2, 1.2]);
Multiple Figures 如何绘制多幅图
使用figure指令,每个figure对应一个画布
x = -10:0.1:10;
y1 = x.^2 - 8;
y2 = exp(x);
figure, plot(x,y1);
figure, plot(x,y2);
注意当存在多幅图时gcf指向的时current figure也就是后画的那一幅,gca也一样。
figure指令还可以指定生成画布的position和size
figure('Position', [left, bottom, width, height]);
Several small plots “in a figure”
subplot(m, n,1); %选择第一块区域
t = 0:0.1:2*pi; x = 3*cos(t); y = sin(t);
subplot(2, 2, 1); plot(x, y); axis normal
subplot(2, 2, 2); plot(x, y); axis square
subplot(2, 2, 3); plot(x, y); axis equal
subplot(2, 2, 4); plot(x, y); axis equal tight
Control of Grid, Box, and Axis
Saving Figures into Files 保存图片
saveas(gcf,'<filename>','<formattype>');
<formattype> 是保存图片的格式,有以下几种: