Matlab(科伊恩)学习5——进阶绘图

五、进阶绘图

  1. 特殊绘图

    1.1 log对数绘图

    >> x=logspace(-1,1,100); #logspace(-1,1,100)-等分取e-1~e+1里面的100个数
       y=x.^2;
       subplot(2,2,1);
       plot(x,y);title('Plot'); 
       subplot(2,2,2);
       semilogx(x,y);title('Semilogx'); #以x轴取对数
       subplot(2,2,3);
       semilogy(x,y);title('Semilogy'); #以y轴取对数
       subplot(2,2,4);
       loglog(x,y);title('Loglog'); #x、y轴均取对数
       set(gca,'XGrid','on'); #打开x轴的格线
       #在log绘图中,常常要打开格线
    

    在这里插入图片描述

    1.2 plotyy()-双y轴

    x = 0:0.01:20;
    y1 = 200*exp(-0.05*x).*sin(x); #指数x正弦→阻尼振动
    y2 = 0.8*exp(-0.5*x).*sin(10*x);
    [AX,H1,H2] = plotyy(x,y1,x,y2); 
    #返回Ax中创建的两个坐标轴的句柄以及H1和H2中每个图形绘图对象的句柄。AX(1)左侧轴,AX(2)右侧轴
    set(get(AX(1),'Ylabel'),'String','Left Y-axis')  #调用y1的x句柄
    set(get(AX(2),'Ylabel'),'String','Right Y-axis') #调用y2的x句柄
    title('Labeling plotyy');
    set(H1,'LineStyle','--'); set(H2,'LineStyle',':');
    
    #替换用法:yyaxis,相对于句柄会方便很多
    yyaxis left; #激活左侧y轴
    plot(x, y1,'--');
    ylabel('left');
    title('title');
    yyaxis right;  #激活右侧y轴
    plot(x,y2,':');
    ylabel('right');
    

    在这里插入图片描述

    1.3 直方图-hist()

    y = randn(1,1000);  #randn-系统自动生成一组按正态分布的随机数
    subplot(2,1,1);
    hist(y,10);
    title('Bins = 10');
    subplot(2,1,2);
    hist(y,50);
    title('Bins = 50');
    #hist(x,n)-matlab函数基于向量x中的元素创建直方图,x中元素有序划入x轴上等距的n个bin中
    

    在这里插入图片描述

    1.4 条形图-bar()

    x = [1 2 5 4 8]; y = [x;1:5];
    subplot(2,3,1); 
    bar(x); title('A bargraph of vector x');
    subplot(2,3,2); 
    bar(y); title('A bargraph of vector y');
    subplot(2,3,3); 
    bar3(y); title('A 3D bargraph');  #bar3-3D图
    subplot(2,3,4); 
    bar(y,'stacked'); title('Stacked');  #stacked-堆积
    subplot(2,3,5); 
    barh(y); title('Horizontal')  #barh-水平
    #bar3与barh可以结合使用,bar3h,组合灵活
    

    在这里插入图片描述
    1.5 饼图-pie()

    a = [10 5 20 30];
    subplot(1,3,1); pie(a); 
    subplot(1,3,2); pie(a, [0,0,0,1]); #0和1表示是否与其他扇形分开
    subplot(1,3,3); pie3(a, [0,0,0,1]); #pie3()-3D图
    

    在这里插入图片描述

    1.6 极坐标图-polar(theta,r)

    x = 1:100; theta = x/10; r = log10(x);
    subplot(1,4,1); polar(theta,r);
    theta = linspace(0, 2*pi); r = cos(4*theta);
    subplot(1,4,2); polar(theta, r);
    theta = linspace(0, 2*pi, 6); r = ones(1,length(theta));#ones(a,b)-生成axb全1数组
    subplot(1,4,3); polar(theta,r);
    theta = linspace(0, 2*pi); r = 1-sin(theta);
    subplot(1,4,4); polar(theta , r);
    

    在这里插入图片描述

    1.7 阶梯图(Stairs)、火柴图(Stem)

    x = linspace(0, 4*pi, 40); y = sin(x);
    subplot(1,2,1); stairs(y);
    subplot(1,2,2); stem(y);
    

    在这里插入图片描述

    *1.8 箱线图、误差图

  2. 色域

    2.1 颜色填充-fill()

    t =(1:2:15)'*pi/8; x = sin(t); y = cos(t);
    fill(x,y,'r'); axis square off; #fill(x,y,c)-以c颜色进行填充
    text(0,0,'STOP','Color', 'w', 'FontSize', 80, ...
    'FontWeight','bold', 'HorizontalAlignment', 'center');
    

    在这里插入图片描述

    2.2 RGB配色

    RGB配色,以向量形式分别配置 R(红)、G(绿)、B(蓝) ,0为最小,1为最大。将0-1分成8个字节(0~255),并且转换为16进制

    [255 0 0]-红色; [0 255 0]-绿色; [0 0 255]-蓝色

    在这里插入图片描述

    2.3 imagesc()、meshgird()

    meshigrid-网格,主要使用的函数为[X,Y]=meshgrid(xgv,ygv);

    meshgrid函数生成的X,Y是大小相等的矩阵,xgv,ygv是两个网格矢量,xgv,ygv都是行向量。

    X:通过将xgv复制length(ygv)行(严格意义上是length(ygv)-1行)得到

    Y:首先对ygv进行转置得到ygv’,将ygv’复制(length(xgv)-1)次得到

    >> [X,Y] = meshgrid(1:3,10:14)
    X =
     
         1     2     3
         1     2     3
         1     2     3
         1     2     3
         1     2     3
      
    Y =
     
        10    10    10
        11    11    11
        12    12    12
        13    13    13
        14    14    14
    

    imagesc(A)-将矩阵A中的元素数值按大小转化为不同颜色,并在坐标轴对应位置处以这种颜色染色

    >> [x, y] = meshgrid(-3:.2:3,-3:.2:3);
       z = x.^2 + x.*y + y.^2; surf( x, y, z); box on;
       set(gca,'FontSize', 16); zlabel('z');
       xlim([-4 4]); xlabel('x'); ylim([-4 4]); ylabel('y');
       
    >> imagesc(z); axis square; xlabel('x'); ylabel('y');
    

    在这里插入图片描述

    imagesc使用时通常搭配 colorbar 使用,用于显示色条,表明颜色与深度的关系,此外还有 colormap(hot/cool/gray)冷暖灰色系

    >> colorbar;
    >> colormap(hot);
    >> colormap(cool);
    >> colormap(gray);
    

    在这里插入图片描述

    #colormap内置名

    color map是一个256x3的矩阵,可以通过自定义来决定colormap

    a=ones(256,3);
    colormap(a);
    
  3. 3D绘图

    3.1 plot3()

    >> plot3(x,y,z);
    

    3.2 3D曲面图常用法则

    ​ 1)通常用来绘制 z=f(x,y)

    ​ 2)需要给matlab提供一系列的(x,y,z)

    ​ 3)使用meshgrid(x,y)来生成给定范围的矩阵X和Y

    3.3 曲面图绘制-mesh(网格)surf(曲面)

    x = -3.5:0.2:3.5; y = -3.5:0.2:3.5;
    [X,Y] = meshgrid(x,y);
    Z = X.*exp(-X.^2-Y.^2);
    subplot(1,2,1); mesh(X,Y,Z);
    subplot(1,2,2); surf(X,Y,Z);
    

    在这里插入图片描述

    3.4 等高线-contour()

    x = -3.5:0.2:3.5; y = -3.5:0.2:3.5;
    [X,Y] = meshgrid(x,y);
    Z = X.*exp(-X.^2-Y.^2);
    subplot(1,2,1);
    mesh(X,Y,Z); axis square;
    subplot(1,2,2);
    contour(X,Y,Z); axis square;
    

    在这里插入图片描述

    >> contour(Z,[起:步长:止])-设置等高线密集程度
    >> [a,b]=contour(Z)-返回a,b,通常用于绘制标注,使用clabel(a,b)
    >> contourf(Z)-填充了颜色的二维等高线
    

    3.5 meshc、surfc

    在绘制网格/曲面图的基础上,在xoy平面上绘制等高线

    3.6 设置视角-view

    sphere(50); #sphere(n)-生成n个面的球体
    shading flat; #shading-设置颜色着色属性
    light('Position',[1 3 2]); #light-创建光源对象
    light('Position',[-3 -1 3]);
    material shiny; #material-设置材料属性
    axis vis3d off; #关闭3D显示
    set(gcf,'Color',[1 1 1]);
    view(-45,20); #设置视角
    

    view(az,el)-az是azimuth(方位角)的缩写,EL是elevation(仰角)的缩写,它们均以度为单位

    在这里插入图片描述

    3.7 光线-light()

    L=light('Position',[-1,-1,1]); #设置light位置,并且将light句柄返回给L
    set(L,'Color','g'); #通过L句柄设置light的颜色
    

    3.8 平面着色-patch()

    patch(‘PropertyName’,propertyvalue,…)

    利用指定的属性/值参数对来指定补片对象的所有属性。除非用户显式的指定FaceColor和EdgeColor的值,否则,MATLAB会使用缺省的属性值。该调用格式允许用户使用Faces和Vertices属性值来定义补片。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值