欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > 【Matlab教程】深入探究图形绘制:基础绘图、图形对象参数

【Matlab教程】深入探究图形绘制:基础绘图、图形对象参数

2024/10/24 12:20:29 来源:https://blog.csdn.net/2401_83283514/article/details/140959481  浏览:    关键词:【Matlab教程】深入探究图形绘制:基础绘图、图形对象参数

1、基础绘图

MATLAB有一个强大的绘制引擎,可以生成各种各样的绘图。

d7f98b963943448cb35a792065495e26.png

1.1根据数据绘图

1、在特定范围内生成函数的数值

2、以图形方式显示数据“点”

1.1.1、plot()

plot(x,y)

plot(y)%若只给y那么x=[1...n],n=length(n)

例如:

plot(cos(0:pi/20:2*pi));

ca969814f2904ce79edd6a1dabbe1235.png

1.1.2、hold on / off  

Matlab在绘图时,会把旧的图形删除掉,使用hold on将两个图形绘制在一个figure中

hold on
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off

5e2597bfb1cb434d9f50ab8b2724697a.png

 1.2、绘图形式

数据标识线型颜色
.-k
*--b
x-.c
+:r

更多详情可参照: 官方文档

 例如:

hold on
plot(cos(0:pi/20:2*pi),'or--');
plot(sin(0:pi/20:2*pi),'xg:');
hold off

 00e060d0ecf14d808784219e851599e9.png

 1.2.1、legend()

若我们想添加注释到图中

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');

19016f77716f4850a82dce368c40d96e.png

1.2.2、title()和label()

通过此函数我们可以为图形加上表头,横轴名称,纵轴名称等。 

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()

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]);

814aa7e475d643dab0687eecaba77ff0.png

例题:将f绘制一条黑线,将g绘制一系列红色范围在t=1到2的圆圈。

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”即辨识码 

· 实用功能:

FunctionPurpose
gcareturn the handle of the "current" axes
gcfreturn the handle of the "current" figure
allchildfind all children of specified objects
ancestorfind ancestor of graphics object
deletedelete an object
findallfind all graphics objects

2.1.2、Fetching or Modifying Properties

· 获取物件:

get() 

x=linspace(0,2*pi,1000);
y=sin(x);plot(x,y);
h=plot(x,y);
get=(h);

73f7f33adebd4b8b984ead5c33dfb0a6.png · 修改物件:

set() 

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

· 标记的面部和边缘颜色:

x=rand(20,1);set(gca,'FontSize',18);
plot(x,'-md','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10);
xlim([1,20])

01618cf7d41e429c8363be2c16ad79bc.png

2.2、Multiple Figures 

· 通过调用figure创建一个窗口:

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); 

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>');

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com