matlab05-初阶绘图

本文详细介绍了MATLAB中的plot函数及其各种绘图风格,包括如何在同一坐标轴上绘制多条曲线以及设置图例、标题和坐标轴标签。此外,还探讨了图形对象属性的修改,如改变线条样式、颜色和标记,并展示了如何通过对象句柄来操作图形元素,如调整坐标轴限制。最后,提供了一些实践例子,如修改轴属性、设置标记样式和实现特定图形效果。
摘要由CSDN通过智能技术生成

一:Basic plotting——plot

  1. plot(x,y)
  2. plot(y) 横坐标默认为1,2,3,4,5…
plot(cos(0:pi/20:2*pi));

请添加图片描述

hold on%实现在一个图框里面绘制多张图
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off

请添加图片描述

  1. Plot style

plot(x,y,‘dataLineColor’)
请添加图片描述

 plot(cos(0:pi/20:2*pi),'d:m');
 plot(cos(0:pi/20:2*pi),'p-r');

请添加图片描述
请添加图片描述

  1. plot在同一个坐标轴画多个图
plot(x,y,str1,x,y2,str2,x,y3,str3....)
等价于
hold on 
plot(x,y,str1)
plot(x,y2,str2)
plot(x,y3,str3)
...
hold off

5.注解function

  • legend(‘L1’,‘L2’,‘L3’…)

给图像标注并引入图例——顺序遵循plot画图的顺序

  • title(str)
  • xlabel(str)
  • ylabel(str)
  • zlabel(str)
x=0:0.1:2*pi;
y1=sin(x);
y2=exp(-x);
plot(x,y1,'p--g',x,y2,'*:m');
xlabel('t = 0 to 2\pi');
ylabel('values of sin(t) and e^{-x}');
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');

请添加图片描述

  1. text()

text(x位置,y位置,str,‘Interpreter’,‘latex’);

  1. annotation()

annotation(‘arrow’,‘X’,[start1,end1],‘Y’,[start2,end2])

数字表示占整张图片的百分比

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

请添加图片描述

练习

t=1:0.01:2;
f=t.^2;
g=sin(2*pi*t);
plot(t,f,'-b',t,g,'or');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pi t)');

请添加图片描述

二:Graphical object properties

1.object hierarchy

Figure
Axes
Line
Text
...

2.手动右击图片编辑打开属性检查器,可以看到不同object的属性列表
请添加图片描述
3.代码修改不同object的属性

先绘个简单的图

x=0:pi/20:2*pi;
y=sin(x);
plot(x,y);
ylabel('f(t)=sin(t)');
xlabel('t');

请添加图片描述

  1. Identify the handle(id) of each object
objecthandle
axisgca
figuregcf
lineh=plot()

other functions of object

allchild返回一个object的所有孩子
ancestor返回一个object的所有祖先
delete (handle)删除一个object
findall返回所有objects
  1. Fetch or alter the object’s properties
functionmeaning
get(handle)返回handle指向的object的所有properties
set(handle,property,属性值)更改handle指向的object的指定property的值
get(h);
get(gca);
get(gcf);

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

set(gca,'XLim',[0,2*pi]);
set(gca,'YLim',[-1.2,1.2]);

请添加图片描述

4. 练习
a.修改Axis的 font (字体)和 tick(记号)

set(gca,'FontSize',25);

请添加图片描述

set(gca,'XTick',0:pi/2:2*pi);%短横线位置
set(gca,'XTickLabel',0:90:360);%短横线内容

请添加图片描述
请添加图片描述

set(gca,'FontName','TeX');
set(gca,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'});

请添加图片描述

set(h,'LineStyle','-.','LineWidth',7.0,'Color','g');

请添加图片描述


b.plot()时,指定marker

x=rand(20,1);
set(gca,'FontSize',18);
plot(x,'md','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10);
set(gca,'XTick',0:5:20);
set(gca,'YTick',0:0.2:1); 

请添加图片描述
c.实现下图转换

请添加图片描述

t=1:0.01:2;
f=t.^2;
g=sin(2*pi*t);
hold on
h1=plot(t,f);
h2=plot(t,g);
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2*\pi t)');
set(gca,'FontSize',16);
set(h1,'LineWidth',2);
set(h2,'Marker','o');

请添加图片描述


5.figure
1.画多幅图在不同的figure

figure,plot(x,y);
figure,plot(y1);

在没有指定情况下,gca ,gcf等所有操作,指代的都是cuttent figure

2.分区画图,一个figure有很多子图

subplot(m,n,d); %1<=d<=m*n
plot(x,y);
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;

请添加图片描述
请添加图片描述
6.保存figure

saveas(gcf,'<filename>','<formattype>');

请添加图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值