1、基础绘图
MATLAB有一个强大的绘制引擎,可以生成各种各样的绘图。
1.1根据数据绘图
1、在特定范围内生成函数的数值
2、以图形方式显示数据"点"
1.1.1、plot()
plot(x,y)
plot(y)%若只给y那么x=[1...n],n=length(n)
例如:
Matlab
plot(cos(0:pi/20:2*pi));
1.1.2、hold on / off
Matlab在绘图时,会把旧的图形删除掉,使用hold on将两个图形绘制在一个figure中
Matlab
hold on
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off
1.2、绘图形式
|------|----|----|
| 数据标识 | 线型 | 颜色 |
| . | - | k |
| * | -- | b |
| x | -. | c |
| + | : | r |
更多详情可参照: 官方文档
例如:
Matlab
hold on
plot(cos(0:pi/20:2*pi),'or--');
plot(sin(0:pi/20:2*pi),'xg:');
hold off
1.2.1、legend()
若我们想添加注释到图中
Matlab
x=0:0.5:4*pi;
y=sin(x);h=cos(x);w=1./(1+exp(-x));
g=(1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2));
plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,g,'c^-');
legend('sin(x)','cos(x)','sigmoid','gauss function');
1.2.2、title()和label()
通过此函数我们可以为图形加上表头,横轴名称,纵轴名称等。
Matlab
x=0:0.1:2*pi;y1=sin(x);y2=exp(-x);
plot(x,y1,'--*',x,y2,':o');
xlabel('t=0 to pi/2');
ylabel('values of sin(t) and exp(-t)');
title('function plots of sin(t) and exp(-t)');
legend('sin(x)','exp(-x)');
1.2.3、text()和annotation()
Matlab
x=linspace(0,3);y=x.^2.*sin(x);plot(x,y);
line([2,2],[0,2^2*sin(2)]);
str='$$ \int_{0}^{2} x^2\sin(x) dx $$';
text(0.25,2.5,str,'Interpreter','latex');
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);
例题:将f绘制一条黑线,将g绘制一系列红色范围在t=1到2的圆圈。
Matlab
t=1:0.01:2;
f=t.^2;
g=sin(2*pi*t);
plot(t,f,'-k',t,g,'or');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pit)');
2、图形对象参数
2.1、Modifying properties of An Object
策略:
1、识别对象的"handle"
2、获取或修改对象的属性
2.1.1、Identifying the Handle of An Object
· 在创建时:
h=plot(x,y);%此时可以获取到"handle"即辨识码
· 实用功能:
|----------|-------------------------------------------|
| Function | Purpose |
| gca | return the handle of the "current" axes |
| gcf | return the handle of the "current" figure |
| allchild | find all children of specified objects |
| ancestor | find ancestor of graphics object |
| delete | delete an object |
| findall | find all graphics objects |
2.1.2、Fetching or Modifying Properties
· 获取物件:
get()
Matlab
x=linspace(0,2*pi,1000);
y=sin(x);plot(x,y);
h=plot(x,y);
get=(h);
· 修改物件:
set()
Matlab
x=linspace(0,2*pi,1000);
y=sin(x);plot(x,y);
h=plot(x,y);
get=(h);
set(gca,'XLim',[0,2*pi]);
set(gca,'YLim',[-1.2,1.2]);
%setting fond and tick of axes
set(gca,'FontSize',25);
set(gca,'XTick',0:pi/2:2*pi);
set(gca,'XTickLabel',0:90:360)
set(gca,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'})
2.1.3、Line Specification
· 线的类型和宽度:
set(h, 'LineStyle' ,'-.' ,'LineWidth', 7.0, 'color', 'g');
2.1.4、Marker Specification
· 标记的面部和边缘颜色:
Matlab
x=rand(20,1);set(gca,'FontSize',18);
plot(x,'-md','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10);
xlim([1,20])
2.2、Multiple Figures
· 通过调用figure创建一个窗口:
Matlab
x=-10:0.1:10;
y1=x.^2-8;
y2=exp(x);
figure,plot(x,y1);
figure,plot(x,y2)
2.2.1、指定Figures的位置和大小:
figure('Position',[left,bottom,width,height]);
2.2.2、在一个figure中创建多个小figure:
subplot(m,n,1);
Matlab
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;
grid on%打开网格线
2.3、保存图像到文件
saveas (gcf,'<filename>',<format type>');