Matlab初阶绘图(2022b)

Matlab初阶绘图(2022b)

绘图函数

plot( ) & others

  1. plot(x,y) plots each vector pairs (x,y)

  2. plot(y) plots each vector pairs (x,y), where x=[1 2 … n]

    Example: plot(cos(0:pi/10:2*pi));​​

    cos

  3. if you want to draw 2 or plots, use hold on/off​​ to have both plots in one figure.

    hold on
    plot(cos(0:pi/20:2*pi));
    plot(sin(0:pi/20:2*pi));
    hold off
    

    ​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XQAcAZBG-1671454736580)(assets/image-20221219113231-bt95b2v.png)]​

  4. about plot style:​ plot(x,y,'str')​​ plots each vector pairs (x,y) using the format defined in str​​.

    ​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LLGnLL5F-1671454736581)(assets/image-20221219113600-hjazlkw.png)]​

    hold on
    plot(cos(0:pi/20:2*pi),'or--');
    plot(sin(0:pi/20:2*pi),'^b:');
    hold off
    
    % codes below are wrong! -->
    plot(cos(0:pi/20:2*pi),'or--',sin(0:pi/20:2*pi),'^b:')
    %错误使用 plot
    %数据必须为单个 y 值输入或者一对或多对 x 和 y 值。
    

POLISHMENT

  1. Add legend to graph legend('L1',...)

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

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s1YeX2s0-1671454736581)(assets/image-20221219151308-wvzw81j.png)]

    Lack ‘name’, title, xlable, ylable …

    Make a little adjustment !

    legend('sin(x)','cos(x)','Sigmoid','Gauss function');
    xlabel('x=0 to x=4\pi');
    ylabel('values of different functions');
    title('Function Plots');
    % 引号内可以使用Markdown写法
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j3tYhpDk-1671454736582)(assets/image-20221219152409-ymjxse4.png)]​

  2. text( ) & annotation( )

    x = linspace(0,3); 
    % x的取值范围
    y = x.^2.*sin(x); 
    plot(x,y);
    line([2,2],[0,2^2*sin(2)]);
    % 括号内前者为line的x坐标范围,后者同理,为y取值范围
    str = '$$\int_{0}^{2} x^2\sin(x) \textup dx$$';
    text(0.25,2.5,str,'Interpreter','latex');
    % Latex写法好麻烦啊,$$转义,'\test'貌似不能用
    % 前两个数为公式起始点坐标(自变量的值)
    annotation('arrow','X',[0.32,0.45],'Y',[0.6,0.4]);
    % XY后的范围貌似指的是在图窗中的比例(即横纵都为1,起点与坐标原点重合)
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dKTtAJ8E-1671454736583)(assets/image-20221219155126-xlah4jg.png)]

  3. Excercise

    t=linspace(1,2);
    % t=1:0.1:2;
    % 找不同
    f=t.^2;
    % 从矩阵运算角度理解为啥要用点乘幂。。。
    g=sin(2*pi*t);
    plot(t,f,'k-',t,g,'ro');
    legend('t^2','sin2\pit');
    xlabel('Time(ms)');
    ylabel('f(t)');
    title('Mini Assignment #1');
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZuizM8sp-1671454736583)(assets/image-20221219161434-cbfnatu.png)]​

Figure Adjustment

Several properties:

Font & Size, Line width, Axis limit, Tick position, Tick label

​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EguzDAGd-1671454736584)(assets/image-20221219161727-a2lb2zx.png)]​

Identify the Handle of An Object

When create h = plot(x,y);​ ‘h’ is the handle of this ‘line’ of function.

​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-spjkdeol-1671454736584)(assets/image-20221219164700-8f73rp0.png)]

To fetch properties, use get ( )​ ; To modify properties, use set ( )

Setting examples:

Axis:

x = linspace(0, 2*pi, 1000);
% 1000为点的数量,不输默认100
y = sin(x); h = plot(x,y);
get(h)

​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3cxxVkk5-1671454736585)(assets/image-20221219170023-njdo3lr.png)]

...
% codes above
set(gca, 'XLim', [0, 2*pi]);
set(gca, 'YLim', [-1.2, 1.2]);
% modify the length of axis
set(gca, 'FontSize', 25);
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', 0:90:360);
% xtick 的替换
set(gca, 'FontName', 'sym');
set(gca, 'XTickLabel', {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'});
% symbol(sym)

​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iEkIKuVb-1671454736585)(assets/image-20221219174123-pq4k3mv.png)]​

Line:

...
set(h, 'LineStyle', '-.','LineWidth', 7.0, 'Color', 'g');
% another choice ->
% plot(x,y, '-.g','LineWidth', 7.0);

​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wofA2sST-1671454736585)(assets/image-20221219174157-ojqjbpp.png)]​

Marker:

x=rand(20,1); set(gca, 'FontSize', 18);
plot(x,'-md','LineWidth', 2, 'MarkerEdgeColor', 'k','MarkerFaceColor', 'g', 'MarkerSize', 10);
xlim([1, 20]);
% equal to 'set(gca, 'XLim', [1,20]);'

​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hXLFJzGH-1671454736586)(assets/image-20221219175006-cgws74p.png)]​

Other applications

存在多图时进行图操作,操作的是当前图 (current plot), 例如最后一张图上的操作。

draw many lines in different axis:

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

position:

​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u8XbvbIo-1671454736586)(assets/image-20221219203658-jl4t7ac.png)]

draw many plots in one window:

subplot( )

​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r7IZtDw2-1671454736587)(assets/image-20221219203929-m9t2fz4.png)]​

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

在这里插入图片描述

​[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V7gbkofc-1671454736587)(assets/image-20221219204318-cm2htuj.png)]​

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值