Matlab(科伊恩)学习4——初阶绘图

四、初阶绘图

  1. 数据绘制

    1.1 plot()——绘制图像

    #plot(x,y)-对应每个(x,y);plot(y)-对应的x为 1,2,3...
    
    #Example
    >> plot(cos(0:pi/20:2*pi));
    

    在这里插入图片描述

    >> plot(cos(0:pi/20:2*pi));
       plot(sin(0:pi/20:2*pi)); #matlab在默认作图时,会覆盖上一张图像,即此处的cos,只显示sin图像
    

    1.2 hold on/off——保留/覆盖

    #通过hold on使多个曲线保留在一个图像上,而hold off刚好相反,取消前图
    >> hold on
       plot(cos(0:pi/20:2*pi));
       plot(sin(0:pi/20:2*pi));
       hold off
    

    在这里插入图片描述

  2. 绘制风格

    2.1 曲线格式

    plot(x,y,'str')-在str中设置格式
    

    在这里插入图片描述 2.2 曲线名称

    legend('L1','L2',...)-标出曲线的名称
    
    #legend里面图例的顺序与plot里面曲线绘制的先后顺序一致,缺省时默认从左到右依次给图例
    >> 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');
    

    在这里插入图片描述 2.3 图像的标题与坐标轴

    title()-添加Figure标题; ?label()-添加坐标轴名称
    
    #title('str')直接添加
    #xlabel()-添加x轴名称;ylabel()-添加y轴名称;zlabel()-添加z轴名称
    
    >> 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('Function Plots of sin(t) and e^{-x}');
       legend('sin(t)','e^{-x}');
    

    在这里插入图片描述
    2.4 LaTex语法的图像标注

    text()-在某一位置进行text备注 ;annotation()-
    
    #Example
    >> x = linspace(0,3); y = x.^2.*sin(x); plot(x,y);
    
    >> line([2,2],[0,2^2*sin(2)]);
       %line([x1,x2],[y1,y2])-绘制从(x1,y1)→(x2,y2)的一条直线
       
    >> str = '$$ \int_{0}^{2} x^2\sin(x) dx $$';  
       % $$...$$-LaTex中表示str的起止;\int-积分;    _0}-下标0;^{2}-上标2;x^2/sinx-被积函数;   dx-微分符号
       
    >> text(0.25,2.5,str,'Interpreter','latex');                                              %text[x,y,字符,'Interpreter','latex'],'Interpreter'与'latex'固定用法,表示LaTex语法
       
    >> annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);
       %'arrow'-标注为箭头形式;'X','Y'-箭头位置相对整个方框的比例,[0.32,0.5]-方框32%→50%区域、   [0.6,0.4]-方框60%→40%区域
    

    在这里插入图片描述

    2.5 Figure微调

    一些配置:

    字体;字号;线宽;Axis limit-坐标轴限制;Tick position;Tick label(Tick指的坐标轴上一个个单位区间)

    2.6 绘图项目

    在这里插入图片描述

    2.6.1 Figure性质

    在这里插入图片描述

    2.6.2 确定目标的句柄

    1)自定创建

    >> h=plot(x,y);  #plot函数会返回line的句柄(辨识码、指针),h指向的就是这一条曲线
    >> gca; #返回Axes的句柄
    >> gcf; #返回Figure的句柄
    

    2)工具函数

    FunctionPurpose
    gcaReturn the handle of the “current” axes
    gcfReturn the handle of the “current” figure
    allchildFind all children of specified objects
    ancestorFind ancestor of graphics object
    deleteDelete an object
    findallFind all graphics objects

    在这里插入图片描述
    2.6.3 获取/修饰性质

    🔸get()-To fetch properties,use
    🔸set()-To modify properties,use
    
    >>x=linspace(0,2*pi,1000);
      y=sin(x); plot(x,y);
      h=plot(x,y);
      get(h)
      
    --→ AlignVertexCenters: 'off'
                Annotation: [1×1 matlab.graphics.eventdata.Annotation]
              BeingDeleted: 'off'
                BusyAction: 'queue'
             ButtonDownFcn: ''
                  Children: [0×0 GraphicsPlaceholder]
                  Clipping: 'on'
                     Color: [0 0.4470 0.7410]
                 ColorMode: 'auto'
                 CreateFcn: ''
           DataTipTemplate: [1×1 matlab.graphics.datatip.DataTipTemplate]
                 DeleteFcn: ''
               DisplayName: ''
          HandleVisibility: 'on'
                   HitTest: 'on'
             Interruptible: 'on'
                  LineJoin: 'round'
                 LineStyle: '-'
             LineStyleMode: 'auto'
                 LineWidth: 0.5000
                    Marker: 'none'
           MarkerEdgeColor: 'auto'
           MarkerFaceColor: 'none'
             MarkerIndices: [1×1000 uint64]
                MarkerMode: 'auto'
                MarkerSize: 6
                    Parent: [1×1 Axes]
             PickableParts: 'visible'
                  Selected: 'off'
        SelectionHighlight: 'on'
                       Tag: ''
                      Type: 'line'
             UIContextMenu: [0×0 GraphicsPlaceholder]
                  UserData: []
                   Visible: 'on'
                     XData: [1×1000 double]
                 XDataMode: 'manual'
               XDataSource: ''
                     YData: [1×1000 double]
               YDataSource: ''
                     ZData: [1×0 double]
               ZDataSource: ''
    
    >> get(gca); #返回Figure参数
    >> get(gcf); #返回Axes参数
    
    #通过返回的参数,我们可以指导line、figure、axes里面的具体参数,进而根据需求进行更改
    

    2.6.4 设置Axes Limits

    >> set(gca,'XLim',[0,2*pi]);
    >> set(gca,'YLim',[-1.2,1.2]);
    
    #Alternative
    >> xlim([0,2*pi]);
    >> ylim([-1.2,1.2]);
    

    2.6.5 设置Font和Tick of Axes

    >> set(gca,'FontSize',25);
    
    >> set(gca,'XTick',0:pi/2:2*pi);   #XTick-坐标轴刻度间距
    >> set(gca,'XTickLabel',0:90:360); #XTickLabel-坐标轴刻度标记
    >> set(gca,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'}); #自定义刻度标记
    

    2.6.6 Line的自定义

    >> set(h,'LineStyle','-.','LineWidth',7.0,'Color','g'); #通过set对plot的句柄h进行设置
    #Alternative
    >> plot(x,y,'-.g','LineWidth',7.0); #在plot时直接设置
    

    2.6.7 标记自定义

    >> x=rand(20,1); 
       set(gca, 'FontSize', 18);
       plot(x,'-md','LineWidth', 2, 'MarkerEdgeColor', 'k',... #edge为驻点边,face为驻点内部
       'MarkerFaceColor', 'g', 'MarkerSize', 10);
       xlim([1, 20]);
    

    在这里插入图片描述

    2.7 多图像绘制——Multiple Figures

    >> x = -10:0.1:10;
       y1 = x.^2 - 8;
       y2 = exp(x);
       figure, plot(x,y1); #通过figure可以同时绘制并弹出多个Figure窗口
       figure, plot(x,y2);
       #此时要慎重使用gca、gcf,它们指向的是最新的Figure
    

    2.8 Figure位置与尺寸设置

    figure('Position',[left,bottom,width,height]); #position的设置,顺便也设置了size
    

    2.9 子绘图区域——Figure里划分子figure

    subplot(m,n,1); # 1 表示的是mxn区块里的位置
    

    在这里插入图片描述

    >>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  #坐标轴与曲线边缘切齐
    

    在这里插入图片描述

    2.10 设置Grid,Box and Axis

    #注意是针对单一的Figure
    grid on/off网格线
    box on/off右上角两根轴
    axis on/off轴线以及背景,只留曲线
    axis normal/ square/ equal/ equal tight见上例
    axis image使Axis与曲线紧密相切
    axis ij矩阵轴,i轴竖直,起点在上;j轴水平,起点在左
    axis xy笛卡尔坐标系,坐标原点再左下角

    2.11 将Figure保存为File

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

    在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值