MATLAB学习笔记(三)——绘图之二维曲线

MATLAB绘图一

二维曲线

plot函数

  1. plot(x,y)

  2. plot(x) 横坐标是向量x的下标

    当x是复数向量,则分别以该向量元素的实部和虚部为横纵坐标绘制

  3. plot(x,y) 函数参数的变化形式

    • 当x是向量,y是矩阵时,

      • 如果y的列数等于x的长度,则以向量x为横坐标,以y的每个行向量为纵坐标绘制曲线,曲线的条数等于y的行数
      • 若y的行数等于x的长度,则以x为横坐标,y的每个列向量为纵坐标绘制曲线,曲线的条数等于y的列数
    • x,y为同型矩阵

      以x,y对应列元素为横、纵坐标分别绘制曲线,曲线条数等于矩阵的列数

  4. 含多个输入参数的plot函数

    plot(x1,y1,x2,y2,……,xn,yn)

  5. 含选项的plot函数

    plot(x,y,选项)

    在这里插入图片描述

fplot函数

  1. 基本用法

    fplot(f,lims,选项) f表示一个函数,通常采用函数句柄的形式

    lims 表示x轴的取值范围,用二元向量[xmin,xmax]描述,默认值为[-5,5]

  2. 双输入函数参数的用法

    fplot(funx,funy,tlims,选项)

    其中,funx,funy代表函数,通常采用函数句柄的形式,tlims为参数函数funx和funy的自变量的取值范围,用二元向量[tmin,tmax]描述

    >> fplot(@(x) sin(1./x),[0,0.2],'b')
    >> plot(x,y)
    >> fplot(@(t) t.*sin(t),@(t) t.*cos(t),[0,10*pi],'r')
    

    fplot函数

绘制图形的辅助操作

图形标注

  • titile (图形标题)

    1. 常规

      >> x=-2*pi:0.05:2*pi;
      >> y=sin(x);
      >> plot(x,y)
      >> plot(x,y);
      >> title('y=sin(x)')
      >> plot(x,y)
      >> title({'MATLAB','y=sin(x)'})
      
    2. 用LaTex格式控制符

      >> title('y=cos{\omega}t')  %y=cosωt
      >> title('y=e^{axt}')		%y=e^axt
      >> title('X_{1}{\geq}X_{2}')%x1>=x2
      >> title('{\bf y=cos{\omega}t+{\beta}}')%y=cosωt+β
      

      基本字符说明:
      \bf 加粗
      \it 斜体
      \rm 正
      \leq <=
      \geq >=

    3. 含属性设置的title函数

      • Color属性

        >> title('y=cos{\omega}t','Color','r')
        
      • FontSize属性 默认字号11

        >>  title('y=cos{\omega}t','FontSize',24)
        
  • xlabel x轴说明

    >> x=-2*pi:0.05:2*pi;
    >> y=sin(x);
    >> plot(x,y)
    >> title('y=sin(x)')
    >> xlabel('-2\pi\leq x\leq 2\pi')
    
  • ylabel y轴说明

  • text x、y图形说明

    text(x,y,说明)

    gtext(说明)

    >> text(-2*pi,0,'-2{\pi}')
    >> text(3,0.28,'\leftarrow sin(x)')
    
  • legend 图例1、图例2……

    >> x=linspace(0,2*pi,100);
    >> plot(x,[sin(x);sin(2*x);sin(3*x)])
    >> legend('sin(x)','sin(2x)','sin(3x)')
    
    50

坐标控制

  1. axis函数

    1. >>axis([-pi,pi,-4,4])  %纵横坐标控制在-4,4之间
      
    2. 其他用法

      • axis equal :纵、横坐标轴采用等长刻度
      • axis square :产生正方形坐标系(默认为矩形)
      • axis auto :使用默认设置
      • axis off :不显示坐标轴
      • axis on : 显示坐标轴
  2. 给坐标系加网格和边框

    • grid on
    • grid off
    • grid 默认无网格
    • box on /off box 默认有边框

图形保持

  1. hold on
  2. hold off

图形窗口的保持

  • 子图:同一图形窗口中的不同坐标系下的图形称为子图

  • subplot函数

    subplot(m,n,p) 其中,m和n指定将图形窗口分为m×n个绘图区,p指当前活动取

图形窗口的分割

在这里插入图片描述

其他形式的二维曲线图

对数坐标图

  1. semilogx(x1,y1,选项1,x2,y2,选项2,···)

  2. semilogy(x1,y1,选项1,x2,y2,选项2,···) 半对数

  3. loglog(x1,y1,选项1,x2,y2,选项2,···) 全对数

    >> x=0:0.1:10;
    >> y=1./x;
    >> subplot(2,2,1);
    >> plot(x,y)
    >> title('plot(x,y)');
    >> subplot(2,2,2);
    >> semilogx(x,y)
    >> title('semilogx(x,y)');grid on
    >> subplot(2,2,3);
    >> semilogy(x,y)
    >> title('semilogy(x,y)');grid on
    >> subplot(2,2,4);
    >> loglog(x,y)
    >> title('loglog(x,y)');grid on
    

    在这里插入图片描述

极坐标图

​ polar(theta,rho,选项)

>> t=0:pi/100:2*pi;
>> r=1-sin(t);
>> subplot(1,2,1)
>> polar(t,r)
>> subplot(1,2,2)
>> t1=t-pi/2;
>> r1=1-sin(t1);
>> polar(t,r1)

在这里插入图片描述

统计图

条形图类图形

  1. 条形图

    bar函数 bar(y,style) y是数据,选项style用于指定分组排列模式

    “grouped” :簇状分组 默认 “stacked”:堆积分组

    >> y=[1,2,3,4,5;1,2,1,2,1;5,4,3,2,1];
    >> subplot(1,2,1)
    >> bar(y)
    >> title('Group')
    >> subplot(1,2,2)
    >> bar(y,'stacked')
    >> title('Stack')
    

    在这里插入图片描述

bar(x,y,style) x存储横坐标,y存储数据

  1. 直方图

    • hist函数 直角坐标系下

      hist(y)

      hist(y,x) y是要统计的数据,x用于指定区间的划分方式

      >> y=randn(500,1);
      >> subplot(2,1,1);
      >> hist(y);
      >> title('高斯分布直方图');
      >> subplot(2,1,2);
      >> x=-3:0.2:3;
      >> hist(y,x);
      >> title('指定区间中心点的直方图')
      

      在这里插入图片描述

    • rose函数 极坐标系下

      rose(theta,x) 用于确定每一区间与原点的角度,x用于指定区间的分布

      >> y=randn(500,1);
      >> theta=y*pi;
      >> rose(theta)
      >> title('在极坐标下的直方图')
      

      在这里插入图片描述

面积类图形

  1. 扇形图

    pie函数 pie(x,explode)

    >> score=[5,17,23,9,4];
    >> ex=[0,0,0,0,1];
    >> pie(score,ex)
    >> legend('优秀','良好','中等','及格','不及格','location','eastoutside')
    

    在这里插入图片描述

  2. 面积图

    area函数

散点类图形

  1. scatter函数:散点图

    scatter(x,y,选项,’filled’)

    >> t=0:pi/50:2*pi;
    >> x=16*sin(t).^3;
    >> y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t);
    >> scatter(x,y,'filled')
    >> scatter(x,y)
    >> scatter(x,y,'rd','filled')
    

    在这里插入图片描述

  2. stairs函数:阶梯图

  3. stem函数:杆图

矢量类图形

  1. compass函数:罗盘图
  2. feather函数:羽毛图
  3. quiver函数:箭头图
    ​ quiver(x,y,u,v)
  4. stairs函数:阶梯图
  5. stem函数:杆图

猜你喜欢

转载自blog.csdn.net/qq_45696618/article/details/113133982
今日推荐