MATLAB学习笔记(4)——初阶绘图

目录

Basic plotting

plot()

hold on&hold off

Plot Style

legend()——添加图例

title() and ?label()

Modifying Properties of An Object

xlim()&ylim()

 Multiple Figures 如何绘制多幅图

Several small plots “in a figure”

Control of Grid, Box, and Axis

 Saving Figures into Files

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> 是保存图片的格式,有以下几种:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值