Matlab中的基本绘图方法

来源:互联网 发布:浙江网络作协 编辑:程序博客网 时间:2024/05/29 14:22

Matlab基础

  • 基本的绘图方法

最近准备数学竞赛需要对Matlab重新进行一个系统的学习,于是将在学习中学到的东西以博客的形式记录一下,这里介绍的是Matlab中的基本的绘图方法


  1. 概论
    对Matlab中的一些基本绘图方法的一个小的概括

    • 二维图形:

      这里写图片描述

      其中的plot等函数的选项描述:

      这里写图片描述

    • 三维图形

      这里写图片描述

    • 专有图形

      这里写图片描述

  2. 实际代码效果

    • 二维图形

      - 特殊二维图形

      • polar semilogx
        这里写图片描述
        -subplot(2,1,1);
        theta = 0:0.1:8*pi;
        polar(theta,cos(4*theta)+1/4);
        subplot(2,1,2)
        x = 0:0.1:2*pi;
        y = sin(x);
        semilogx(x,y);
        grid on;

      • 复数
        这里写图片描述

        -t = 0:0.1:2*pi;
        x = sin(t);
        y = cos(t);
        z = x + i*y;
        subplot(2,1,1);
        plot(t,z);
        subplot(2,1,2)
        plot(z);

      • 双y轴
        这里写图片描述
        -x = 0:0.01:5;
        y = exp(x);
        plotyy(x,y,x,y,'semilogy','plot');
        grid on;

      - 基本二维图形

      • plot

        这里写图片描述

        -y = [3,7,9,1,5,2,8];
        subplot(1,2,1);
        plot(y,'linewidth',2);
        grid on;
        x = [3,3,9;8,1,2;1,8,5;7,9,1];
        subplot(1,2,2);
        plot(x);


  • 三维图形

    • 曲面图

      这里写图片描述
      -subplot(2,2,1);
      sphere(3);
      subplot(2,2,2);
      t = linspace(pi/2,3.5*pi,50);
      R = cos(t)+2;
      cylinder(R,3);
      subplot(2,2,3);
      sphere(50);
      axis equal;
      subplot(2,2,4);
      cylinder(R,50);

    • 曲线图
      这里写图片描述
      -t = -pi:pi/200:8*pi;
      h = figure(1);
      set(h,'color',[1,1,1]);
      subplot(1,2,1);
      plot3(cos(t),sin(t),t,'b -');
      subplot(1,2,2);
      plot3(sin(t),cos(t),t,'.');

    • 网格图
      这里写图片描述
      -[x,y] = meshgrid(-8:0.5:8,-10:0.5:10);
      R = sqrt(x.^2 + y.^2) + eps;
      z = sin(R)./R;
      mesh(x,y,z);
      hidden on;

  • 专有图形

    • 饼形图
      这里写图片描述
      -cj = [80,95,70,40];
      subplot(1,2,1);
      pie(cj,[0,0,1,0]);
      subplot(1,2,2);
      pie3(cj,[0,0,1,0],{'语文','数学','英语','政治'});
      colormap(jet)
      %调整文本位置
      %获取位置
      get(findobj(gca,'Type','Text','String','数学'),'Position')
      %调整位置
      set(findobj(gca,'Type','Text','String','数学'),'Position',[-0.6,-1.2,-1])

    • 等高线图
      这里写图片描述
      -subplot(2,1,1);
      x = 1:10;
      y = 11:20;
      z = 21:30;
      [x,y,z] = peaks;
      contour(x,y,z,15);
      subplot(2,1,2);
      contour3(x,y,z,20);

    • 离散数据图
      这里写图片描述
      -subplot(2,1,1);
      x = linspace(0,2*pi,40);
      a = sin(2*x);
      b = cos(x);
      stem(x,a+b);
      hold on;
      plot(x,a+b);
      hold on;
      plot(x,a);
      hold on;
      plot(x,b);
      hold off;
      subplot(2,2,3)
      a1 = x.*a;
      stem3(x,a1,x);
      %阶梯图
      subplot(2,2,4)
      alpha = 0.01;
      beta = 0.5;
      t = 0:10;
      f = exp(-alpha*t).*sin(2*beta*t);
      stairs(t,f);
      hold on;
      plot(t,f,'--*');

    • 瀑布图
      这里写图片描述
      -x = 1:10;
      y = 11:20;
      z = 21:30;
      [x,y,z] = peaks;
      waterfall(peaks)

    • 条形图

      • bar
        这里写图片描述
        -%如果Y是向量的话,就只有一条
        %单输入
        Y = [3,8,2;8,9,2;8,3,3;2,5,6;9,5,1];
        subplot(2,2,1),bar(Y),title('二维条形图');
        subplot(2,2,2),bar3(Y),title('三维条形图');
        %多输入
        X = 1:5;
        Z = [2,3,4,5,6];
        %这里的X必须和Y的行数相匹配
        subplot(2,2,3),bar(X,Z,'red'),title('二维条形图--多输入');
        subplot(2,2,4),bar3(X,Z),title('三维条形图--多输入');

      • barh
        这里写图片描述
        -%如果Y是向量的话,就只有一条
        %单输入
        Y = [3,8,2;8,9,2;8,3,3;2,5,6;9,5,1];
        subplot(2,2,1),barh(Y),title('二维条形图');
        subplot(2,2,2),bar3h(Y),title('三维条形图');
        %多输入
        X = 1:5;
        Z = [2,3,4,5,6];
        %这里的X必须和Y的行数相匹配
        subplot(2,2,3),barh(X,Z),title('二维条形图--多输入');
        subplot(2,2,4),bar3h(X,Z),title('三维条形图--多输入');

      • 额外功能
        这里写图片描述
        -%如果Y是向量的话,就只有一条
        %单输入
        Y = [3,8,2;8,9,2;8,3,3;2,5,6;9,5,1];
        subplot(2,2,1),bar(Y,'grouped','red'),title('二维条形图');
        subplot(2,2,2),bar3(Y,'stacked'),title('三维条形图');
        %不加限定
        subplot(2,2,3),bar(Y),title('二维条形图');
        subplot(2,2,4),bar3(Y),title('三维条形图');

    • 直方图

      • hist
        这里写图片描述
        -y1 = randn(10000,1);
        y2 = rand(10000,1);
        subplot(3,2,1);
        hist(y1,20);
        title('正态分布--带m');
        subplot(3,2,2);
        hist(y2,20);
        title('均匀分布--带m');
        %默认10
        subplot(3,2,3);
        hist(y1);
        title('正态分布--无参');
        subplot(3,2,4);
        hist(y2);
        title('均匀分布--无参');
        %增加中间值
        x = 1:100;
        subplot(3,2,5);
        hist(y1,x);
        title('正态分布--带中间值');
        subplot(3,2,6);
        hist(y2,x);
        title('均匀分布--带中间值');

      • rose
        这里写图片描述
        -y1 = randn(10,1)*pi;
        y2 = rand(10,1)*pi;
        subplot(3,2,1);
        rose(y1,20);
        title('正态分布--带m');
        subplot(3,2,2);
        rose(y2,20);
        title('均匀分布--带m');
        %默认10
        subplot(3,2,3);
        rose(y1);
        title('正态分布--无参');
        subplot(3,2,4);
        rose(y2);
        title('均匀分布--无参');
        %增加中间值
        x = 1:100;
        subplot(3,2,5);
        rose(y1,x);
        title('正态分布--带中间值');
        subplot(3,2,6);
        rose(y2,x);
        title('均匀分布--带中间值');

代码:
这里写图片描述

下载地址:http://download.csdn.net/detail/qq_34861102/9922063
原文链接:http://blog.csdn.net/qq_34861102/article/details/76718557

原创粉丝点击