matlab 画图时遇到的一些问题以及解决方法

2023-05-16

matlab 画图时遇到的一些问题以及解决方法

最近在使用 matlab 画图时,遇到了许许多多各式各样的问题,有些问题甚至折腾了很久才搞好,特此记录下来。


  1. 设置画图时图中线段的粗细
    plot(x1,y1,'b-','LineWidth',2);
    
  2. 设置x轴y轴的范围
    axis([0,3000,0,100]);
    
  3. 多张图在一个页面中绘出
    % 绘完每张图后在使用 hold on,并在最后使用 hold off
    plot(x1,y1,'b-','LineWidth',2);
    hold on;
    plot(x2,y2,'r:','LineWidth',2);
    hold off;
    
  4. 设置图例的位置
    详见官方文档:https://ww2.mathworks.cn/help/releases/R2016b/matlab/ref/legend.html?searchHighlight=legend&s_tid=doc_srchtitle#zmw57dd0e451323
    % Location 表示相对于坐标的位置,northwest 表示 左上
    legend('location', 'northwest');
    
  5. 去除图中上方与右方的的边框
    • 方法一:在绘图操作(plot)之前添加 hold on; 即可
      hold on;
      plot(x1,y1,'b-','LineWidth',2);
      
    • 方法二:在绘图操作(plot)之后添加 box off; 即可
      plot(x1,y1,'b-','LineWidth',2);
      box off;
      
  6. 设置 X 轴标签为中文
    set(gca,'XTickLabel',{'第一个','第二个','第三个'});
    
  7. 为柱状图进行形状填充
    使用自封装函数 applyhatch.m,相关链接:https://www.ilovematlab.cn/thread-177581-1-1.html
    • 首先新建函数脚本applyhatch.m

      function applyhatch(h,patterns,colorlist)
      %APPLYHATCHApply hatched patterns to a figure
      % APPLYHATCH(H,PATTERNS) creates a new figure from the figure H by
      % replacing distinct colors in H with the black and white
      % patterns in PATTERNS. The format for PATTERNS can be
      %   a string of the characters '/', '\', '|', '-', '+', 'x', '.'
      %   a cell array of matrices of zeros (white) and ones (black)
      %
      % APPLYHATCH(H,PATTERNS,COLORS) maps the colors in the n by 3
      % matrix COLORS to PATTERNS. Each row of COLORS specifies an RGB
      % color value.
      %
      % Note this function makes a bitmap image of H and so is limited
      % to low-resolution, bitmap output.
      %
      % Example 1:
      %   bar(rand(3,4));
      %   applyhatch(gcf,'\-x.');
      %
      % Example 2:
      %   colormap(cool(6));
      %   pie(rand(6,1));
      %   legend('Jan','Feb','Mar','Apr','May','Jun');
      %   applyhatch(gcf,'|-+.\/',cool(6));
      %
      % See also: MAKEHATCH
      % By Ben Hinkle, 
      % This code is in the public domain. 
      
      oldppmode= get(h,'paperpositionmode');
      oldunits= get(h,'units');
      set(h,'paperpositionmode','auto');
      set(h,'units','pixels');
      figsize= get(h,'position');
      if nargin == 2
      colorlist = [];
      end
      bits= hardcopy(h,'-dzbuffer','-r0');
      set(h,'paperpositionmode',oldppmode);
      bwidth= size(bits,2);
      bheight= size(bits,1);
      bsize= bwidth * bheight;
      if~isempty(colorlist)
          colorlist = uint8(255*colorlist);
          [colors,colori] = nextnonbw(0,colorlist,bits);
      else
          colors = (bits(:,:,1) ~= bits(:,:,2)) | ...
              (bits(:,:,1) ~= bits(:,:,3));
      end
      pati= 1;
      colorind= find(colors);
      while~isempty(colorind)
          colorval(1) = bits(colorind(1));
          colorval(2) = bits(colorind(1)+bsize);
          colorval(3) = bits(colorind(1)+2*bsize);
          if iscell(patterns)
              pattern = patterns{pati};
          elseif isa(patterns,'char')
              pattern = makehatch(patterns(pati));
          else
              pattern = patterns;
          end
          pattern = uint8(255*(1-pattern));
          pheight = size(pattern,2);
          pwidth = size(pattern,1);
          ratioh = ceil(bheight/pheight);
          ratiow = ceil(bwidth/pwidth);
          bigpattern = repmat(pattern,[ratioh ratiow]);
          if ratioh*pheight > bheight
              bigpattern(bheight+1:end,:) = [];
          end
          if ratiow*pwidth > bwidth
              bigpattern(:,bwidth+1:end) = [];
          end
          bigpattern = repmat(bigpattern,[1 1 3]);
          color = (bits(:,:,1) == colorval(1)) & ...
              (bits(:,:,2) == colorval(2)) & ...
              (bits(:,:,3) == colorval(3));
          color = repmat(color,[1 1 3]);
          bits(color) = bigpattern(color);
          if ~isempty(colorlist)
              [colors,colori] = nextnonbw(colori,colorlist,bits);
          else
              colors = (bits(:,:,1) ~= bits(:,:,2)) | ...
                  (bits(:,:,1) ~= bits(:,:,3));
          end
          colorind = find(colors);
          pati = (pati + 1);
          if pati > length(patterns)
              pati = 1;
          end
      end
      newfig= figure('units','pixels','visible','off');
      imaxes= axes('parent',newfig,'units','pixels');
      im= image(bits,'parent',imaxes);
      fpos= get(newfig,'position');
      set(newfig,'position',[fpos(1:2) figsize(3) figsize(4)+1]);
      set(imaxes,'position',[0 0 figsize(3) figsize(4)+1],'visible','off');
      set(newfig,'visible','on');
      
      
      function [colors,out] = nextnonbw(ind,colorlist,bits)
      out = ind+1;
      colors = [];
      while out <= size(colorlist,1)
        if isequal(colorlist(out,:),[255 255 255]) | ...
              isequal(colorlist(out,:),[0 0 0])
          out = out+1;
        else
          colors = (colorlist(out,1) == bits(:,:,1)) & ...
                   (colorlist(out,2) == bits(:,:,2)) & ...
                   (colorlist(out,3) == bits(:,:,3));
          return
        end
      end
      
      % 而applyhatch函数需要调用下面的函数
      
      function A = makehatch(hatch)
      %MAKEHATCH Predefined hatch patterns
      %  MAKEHATCH(HATCH) returns a matrix with the hatch pattern for HATCH
      %   according to the following table:
      %      HATCH        pattern
      %     -------      ---------
      %        /          right-slanted lines
      %        \          left-slanted lines
      %        |          vertical lines
      %        -          horizontal lines
      %        +          crossing vertical and horizontal lines
      %        x          criss-crossing lines
      %        .          single dots
      %
      %  See also: APPLYHATCH
      
      %  By Ben Hinkle,
      %  This code is in the public domain.
      
      n = 6;
      A=zeros(n);
      switch (hatch)
      case '/'
        A = fliplr(eye(n));
      case '\'
        A = eye(n);
      case '|'
        A(:,1) = 1;
      case '-'
        A(1,:) = 1;
      case '+'
        A(:,1) = 1;
        A(1,:) = 1;
      case 'x'
        A = eye(n) | fliplr(diag(ones(n-1,1),-1));
      case '.'
        A(1:2,1:2)=1;
      otherwise
        error(['Undefined hatch pattern "' hatch '".']);
      end
      
      
    • 第二部在需要的地方调用函数

      %为柱状图进行形状填充,\.x- 为填充类型,可继续往后添加,详见 applyhatch.m 源码
      applyhatch(gcf,'\.x-');
      

本文首发在本人博客 : https://blog.gitnote.cn/post/matlab-hua-tu-shi-yu-dao-de-yi-xie-wen-ti-yi-ji-jie-jue-fang-fa/

版权信息: CC BY-NC-SA 4.0 (自由转载-非商用-相同方式共享-保持署名)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

matlab 画图时遇到的一些问题以及解决方法 的相关文章

  • 解决cannot open shared object file: No such file or directory

    一 linux下调用动态库 so文件时提示 xff1a cannot open shared object file No such file or directory 解决办法 xff1a 1 此时ldd xxx查看依赖缺少哪些库 lib
  • cmake 使用(六)

    本文是 cmake 使用的第六篇 主要介绍如何设置编译器优化标志 上一篇的链接为 xff1a https blog csdn net QCZL CC article details 119825737 xff0c 主要介绍如何将自己的软件安
  • 8086寄存器介绍

    8086 有14个16位寄存器 xff0c 这14个寄存器按其用途可分为 1 通用寄存器 2 指令指针 3 标志寄存器和 4 段寄存器等4类 1 通用寄存器有8个 又可以分成2组 一组是数据寄存器 4个 另一组是指针寄存器及变址寄存器 4个
  • C++常用操作符:: -> . (例子详解)

    C 43 43 提供了三种访问类或者类对象的操作符 xff0c 他们是 双冒号 点 箭头 gt 这三种操作符有着各自的使用场景和定义 双冒号 A B 表示作用域运算符 A一定是一个类的名称或命名空间的名称 仅仅用于当B是A类 A命名空间的一
  • STM32中断优先级的分配以及中断原则

    STM32d的中断优先级由NVIC IPRx寄存器来配置 xff0c IPR的宽度为8bit所以原则上每个中断可配置的优先级为0 255 xff0c 数值越小优先级越高 xff0c 但对于大部分的 Cortex M3芯片都会精简设计 xff
  • 晶体管的结构、类型和三种组态

    晶体管有两大类型 双极型晶体管 BJT 和场效应管 FET 双极型晶体管又称为半导体三极管 晶体三极管 xff0c 简称晶体管 它由两个PN结组合而成 xff0c 有两种载流子参与导电是一种电流控制电流源器件 场效应管仅有一种载流子参与导电
  • STM32单片机基础09——重定向printf函数到串口输出的多种方法

    本文详细的介绍了如何重定向printf输出到串口输出的多种方法 xff0c 包括调用MDK微库 xff08 MicroLib xff09 的方法 xff0c 调用标准库的方法 xff0c 以及适用于 GNUC 系列编译器的方法 1 prin
  • STM32直流减速电机控制篇(一)PWM调速

    直流电机原理 下面是分析直流电机的物理模型图 其中 xff0c 固定部分有磁铁 xff0c 这里称作主磁极 xff1b 固定部分还有电刷 转动部分有环形铁芯和绕在环形铁芯上的绕组 直流电机的转动原理我就不再赘述 xff0c 比较简单易懂 直
  • STM32直流减速电机控制篇(二)编码器测速原理

    编码器 编码器是一种将角位移或者角速度转换成一连串电数字脉冲的旋转式传感器 xff0c 我们可以通过编码器测量到底位移或者速度信息 编码器从输出数据类型上分可以分为增量式编码器和绝对式编码器 从编码器检测原理上来分 xff0c 还可以分为光
  • STM32直流减速电机控制篇(三)编码器测速程序编写

    编程思路 任何一个程序的编写我们都应该先理清楚编程思路 xff0c 通过上一篇讲解的编码器测速原理我们应该知道要想通过编码器得知电机转速我们第一步就应该是捕获A相和B相输出的脉冲 因为电机速度的定义是单位时间内的转数 xff0c 所以第二步
  • GPIO模式

    开漏输出 只能输出低电平 xff0c 不能输出高电
  • 单片机485通信

    1 RS485简介 485 xff08 一般称作 RS485 EIA 485 xff09 是隶属于 OSI 模型物理层的电气特性规定为 2 线 xff0c 半双工 xff0c 多点信的标准 它的电气特性和 RS 232 大不一样 用缆线两端
  • Jetson Xavier NX 镜像制作、烧录及克隆

    以下所有方法仅适用于Jetson Xavier Nx 16G emmc版本 其他版本仅供参考 官方文档下载链接为https developer nvidia com embedded downloads search 61 Develope
  • Postman下载,安装,注册及登录教程

    目录 一 Postman简介 二 Postman的注册 1 首先下载Postman xff0c 进入官网 xff1a Download Postman Get Started for Free 2 安装Postman 3 找到所下载的app
  • 一文掌握fastapi微服务开发

    目录 一 概述 1 1 微服务 1 1 1 微服务的优势 1 1 2 微服务的缺点 1 2 为何使用Python开发微服务 1 3 FastAPI概述 二 开发 2 1 安装FastAPI 2 1 1 安装虚拟环境 2 1 2 创建虚拟环境
  • Windows通过SSH连接虚拟机中的ubuntu系统

    zz windows通过ssh连接虚拟机中的ubuntu步骤 音量 博客园
  • PaddleServing图像语义分割部署实践

    目录 一 任务概述 二 官方示例部署 2 1 安装PaddleServing 2 2 导出静态图模型 2 3 转换为serving模型 2 4 启动服务 2 5 客户端请求 三 基于PipeLine的抠图功能部署 3 1 基于深度学习的抠图
  • C/C++资源大全(各种库、框架等)

    转载 https www cplusplus me 2182 html C 43 43 资源大全 各种库 框架等 目录 隐藏 1 标准库2 框架3 人工智能4 异步事件循环5 音频6 生态学7 压缩8 并发性9 容器10 密码学11 数据库
  • 一文掌握面向Windows平台的深度学习工控程序开发(使用Paddle Inference部署MFC、C#程序,内含完整代码链接)

    目录 一 概述1 1 智能制造和飞桨1 2 Paddle Inference工业级应用部署工具 二 算法训练和导出2 1 任务概述和实现原理2 2 训练和静态模型导出 三 部署环境准备四 Windows下C 43 43 工程编译和运行4 1
  • png图片自动转ttf字体(使用python实现)

    这里写目录标题 一 任务概述二 实现2 1 ocr识别2 1 1 安装环境2 1 2 实现脚本 2 2 图形文字精确提取2 3 png转svg2 4 svg转ttf 一 任务概述 任务要求 xff1a 需要将上述生僻字png图片批量自动转成

随机推荐