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

相关推荐
杨福瑞32 分钟前
数据结构:单链表(1)
c语言·开发语言·数据结构
来来走走32 分钟前
kotlin学习 基础知识一览
android·开发语言·kotlin
程序员卷卷狗5 小时前
JVM 调优实战:从线上问题复盘到精细化内存治理
java·开发语言·jvm
lly2024066 小时前
ASP Folder:深入解析其功能与使用技巧
开发语言
雪域迷影6 小时前
Go语言中通过get请求获取api.open-meteo.com网站的天气数据
开发语言·后端·http·golang·get
短视频矩阵源码定制8 小时前
矩阵系统源码推荐:技术架构与功能完备性深度解析
java·人工智能·矩阵·架构
ysdysyn8 小时前
C# 进程管理实战:检查与启动EXE程序的完整指南
开发语言·c#
IDOlaoluo8 小时前
PHP-5.2.1.tar.gz 离线安装教程:从源码编译到配置的详细步骤(附安装包)
开发语言·php
wangjialelele9 小时前
Qt中的常用组件:QWidget篇
开发语言·前端·c++·qt
卍郝凝卍9 小时前
NVR(网络视频录像机)和视频网关的工作方式
网络·图像处理·物联网·音视频·视频解决方案