2021-06-29 MATLAB学习随记——可视化基础

本文中所有命令行需在matlab中运行得到对应图像

一、 二维图形

1. plot

plot(Y)
plot(X1,Y1,...,Xn,Yn)Xn:横坐标数据;Yn:纵坐标数据
plot(X1,Y1,LineSpec,...,Xn,Yn,LineSpec)LineSpec:绘图线属性
plot(...,'PropertyName',PropertyValue)PropertyName:绘图属性名;PropertyValue:绘图属性值
plot(axes_handle,...)axes_handle:坐标轴句柄
h = plot(...)h:绘图后返回的图形句柄

1.1 plot(Y)

命令行:

clear;
y1 = sin((1:100)/100 * pi * 2);
y2 = cos((1:100)/100 * pi * 2);
y3 = [y1' y2'];     %叠加两条曲线
y4 = y1+y2*i;

subplot(221);plot(y1);      %subplot(abc)构建一个a×b大小图形矩阵,将之后的plot放在c位置,c < a×b
subplot(222);plot(y2);
subplot(223);plot(y3);grid on;		%grid on;显示网格线
subplot(224);plot(y4);

1.2 plot(X1,Y1,…,Xn,Yn)

命令行:

clear;
x1 = (1:100)/100*pi*2;
x2 = ((1:100)-20)/100*pi*2;
x3 = [x1' x2'];
y1 = sin((1:100)/100*pi*2);
y2 = cos((1:100)/100*pi*2);
y3 = [y1' y2'];

subplot(221);plot(x1,y1);axis tight;
subplot(222);plot(x1,y3);axis tight;
subplot(223);plot(x3,y3);axis tight;
subplot(224);plot(x1,y2,x3,0.5*y3);axis tight;

axis tight意为设置坐标轴显示范围为紧凑型

1.3 plot(X1,Y1,LineSpec,…,Xn,Yn,LineSpec)

命令行:

clear;
x1 = (1:100)/100*pi*2;
x2 = ((1:100)-20)/100*pi*2;
x3 = [x1' x2'];
y1 = sin((1:100)/100*pi*2);
y2 = cos((1:100)/100*pi*2);
y3 = [y1' y2'];

subplot(131);plot(x1,y1,'k.');axis tight;       %'k.':.描绘图像
subplot(132);plot(x1,y1,'k.',x2,y2,'r+');axis tight;        %'r+':+描绘图像
subplot(133);plot(x1,y2,'k.',x3,0.5*y3,'-r+');axis tight;		%'-r+'+在曲线上描绘点

2. subplot:子图绘制

subplot(m,n,p)在m行n列的第p幅图中绘制当前曲线

2.1 对称子图绘制说明

命令行:

clear;
figure;
subplot(221);
text(.5 ,.5,{'subplot(221)'},'FontSize',14,'HorizontalAlignment','center')
%text(axis_x,axis_y,'字号名',字号,'参数名','参数量')
subplot(222);
text(.5 ,.5,{'subplot(222)'},'FontSize',14,'HorizontalAlignment','center')
subplot(223);
text(.5 ,.5,{'subplot(223)'},'FontSize',14,'HorizontalAlignment','center')
subplot(224);
text(.5 ,.5,{'subplot(224)'},'FontSize',14,'HorizontalAlignment','center')

2.2 非对称子图绘制示例

命令行:

clear;
figure;
subplot(2,2,[1 3])
text(.5,.5,{'subplot(2,2,[1 3])'},'Fontsize',14,'HorizontalAlignment','center');
subplot(222);
text(.5 ,.5,{'subplot(222)'},'FontSize',14,'HorizontalAlignment','center')
subplot(224);
text(.5 ,.5,{'subplot(224)'},'FontSize',14,'HorizontalAlignment','center')

3. 图形叠绘

hold on保留当前轴和图形并接受绘制新图形
hold off不保留当前轴和图形,绘制新图形后,原图形被覆盖
holdhold onhold off语句的切换

命令行:

clear;
x = -pi:pi/20:pi;
figure;     %创建图窗(多个图窗时有用)
hold off;
subplot(121);plot(sin(x));plot(cos(x));axis tight;  %先绘制sin(x)后绘制cos(x)因为hold off所以只保留最新图像

subplot(122);plot(sin(x));
hold on;        %保留sin(x)的图像
plot(cos(x));axis tight;        %sin(x)的基础上绘制cos(x)的图像

4. 交互绘图

使用鼠标进行绘图

ginput
gtext
zoom

4.1 ginput

[x,y] = ginput(n)从二维图形中获得n个点的数据坐标(x,y),n应为正整数

命令行:

clear;
x = -pi:pi/20:pi;
figure;
plot(sin(x));axis tight;
[x,y] = ginput(5)

得到sin(x)的图像后鼠标点击取5个点输出。

4.2 gtext

gtext('string')把字符串放置到图形中作为文字说明
gtext({'string1;'string2';'string3';...})把字符串数组放置到图形中

命令行:

clear;
x = -pi:pi/20:pi;
figure;
plot(sin(x));axis tight;
gtext({'1';'2';'3';'4';'5'})

得到sin(x)的图像后用鼠标标记5个点。

4.3 zoom

命令格式说明
zoom on规定当前图形可以进行缩放
zoom off规定当前图形不可以进行缩放
zoom上两者的相互切换
zoom xon规定当前图形的x轴可以进行缩放
zoom yon规定当前图形的y轴可以进行缩放
zoom out使图形返回初始状态
zoom(factor)设置缩放变焦银子,默认值为2

命令行:

clear;
x = -pi:pi/20:pi;
y = sin(x);

fg1 = figure('numbertitle','off','name','缩放打开');
zoom on;        %缩放打开
plot(y);

fg2 = figure('numbertitle','off','name','缩放关闭');
zoom off;       %缩放关闭
plot(y);

fg3 = figure('numbertitle','off','name','x轴缩放打开');
zoom xon;       %x轴缩放打开
plot(y);

fg4 = figure('numbertitle','off','name','y轴缩放打开');
zoom yon;       %y轴缩放打开
plot(y)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值