matlab——基础绘图

matlab——基础绘图

Basic Plotting

plot()

plot(cos(0:pi/20:2*pi));

hold on/off

hold on            %保留图形
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off           %消除图形

plot style

  • 可在command window中输入 help linespec 查询
    在这里插入图片描述

    hold on %保留图形
    plot(cos(0:pi/20:2pi),‘or–’);
    plot(sin(0:pi/20:2
    pi),‘xg–’);
    hold off %消除图形
    在这里插入图片描述
    legend()

    Add legend to graph
    Position adjustment

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

在这里插入图片描述
title() and ?label()

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

在这里插入图片描述
text() and annotation()

  • text with mathematical expression using LaTex
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]);

在这里插入图片描述
exercise
在这里插入图片描述

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

Figure Adjustment

  • Several properties:
    • Font
    • Font size
    • Line width
    • Axis limit
    • Tick position
    • Tick label

Graphical Objects

  • Figure object
  • Axes object
  • Line object

Figures→Edit→Figure properties→选中figure/axes/line object→more properties

Modifying Properties of An Object

  • Strategy:
    1.Identify the “handle” of an object
    2.Fetch or modify the object’s properties

1.Identify the “handle” of an object

Utility functions:
gca: return the handle of the “current” axes
gcf : return the handle of the “current” figure
allchild : find all children of specified objects
delete : delete an object
findall : find all graphic objects

2.Fetch or modify the object’s properties

getting object properties: get()

x = linspace(0,2*pi,1000);
y = sin(x) ;   plot(x,y);
h = plot(x,y) ;  get(h)
get(gca)       % axes object的一系列信息  

在这里插入图片描述
setting axes limits

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

在这里插入图片描述
Setting Font and Tick of Axes

set(gca,'Fontsize',25);                 %坐标轴的坐标点字号变大
set(gca,'XTick',0:pi/2:2*pi);           
set(gca,'XTickLabel',0:90:360);
set(gca,'FontName','symbol');
set(gca,'XTicklabel',{'0','p/2','p','3p/2','2p'})  

在这里插入图片描述
Line Specification

Line style and width

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

在这里插入图片描述
Try : delete(h)
结果是: line消失,Axes仍在
在这里插入图片描述
Marker Specification

Multiple Figures

create a figure window by calling figure

x = -10:0.1:10;
y1 = x.^2-8;
y2 = exp(x);
figure, plot(x,y1);
figure, plot(x,y2);

在这里插入图片描述
在这里插入图片描述
Be careful when using the gcf handle where there exists muliple figures
(同时画出两个figure,gcf和gca只指向第二个,第一个的properties已经找不到了)

Figure Position and Size

figure(‘Position’,[left,bottom,width,height]);
-在这里插入图片描述
Several Plots in One Figure

  • several small plots “in a figure”

    subplot(m,n,1);
    m:number of rows
    n: number of columns
    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          %表示x轴和y轴的单位长度相同
subplot(2,2,4); plot(x,y) ; axis equal tight    %设置坐标轴的范围为数据的范围且横轴纵轴单位长度相同

在这里插入图片描述Control of Grid,Box,and Axis
grid on/off Make the grid visible or invisible
box on/off Make the box visible or invisible
axis on/off Make the axes visible or invisible
axis normal Automatically adjust the aspect ratio of axes and the relative scaling of the data units
axis squal Make the aspect ratio so that the data units are the same in every direction
axis equal tight Set the axis limits to the range of data
axis image Let the plot box fits tightly around the data
axis ij Place the origin of the coordinate system in the upper left corner
axis xy Place the origin in the lower left corner

Saving Figures into Files
saveas(gcf,’< filename>’,’< formattype>’);

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值