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

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

相关推荐
念念01072 小时前
数学建模竞赛中评价类相关模型
python·数学建模·因子分析·topsis
四维碎片2 小时前
【Qt】线程池与全局信号实现异步协作
开发语言·qt·ui·visual studio
IT码农-爱吃辣条2 小时前
Three.js 初级教程大全
开发语言·javascript·three.js
☺����3 小时前
实现自己的AI视频监控系统-第一章-视频拉流与解码2
开发语言·人工智能·python·音视频
染翰3 小时前
lua入门以及在Redis中的应用
开发语言·redis·lua
王者鳜錸3 小时前
PYTHON让繁琐的工作自动化-函数
开发语言·python·自动化
兔老大RabbitMQ4 小时前
git pull origin master失败
java·开发语言·git
tt5555555555554 小时前
C/C++嵌入式笔试核心考点精解
c语言·开发语言·c++
xiao助阵4 小时前
python实现梅尔频率倒谱系数(MFCC) 除了傅里叶变换和离散余弦变换
开发语言·python
云手机掌柜4 小时前
Tumblr长文运营:亚矩阵云手机助力多账号轮询与关键词布局系统
大数据·服务器·tcp/ip·矩阵·流量运营·虚幻·云手机