【MATLAB基础绘图第2棒】绘制柱状/饼图填充图

news2025/3/11 7:04:47

MATLAB绘制柱状填充图

  • 方法1:hatchfill2工具
    • 1.1 案例1:柱状图填充
    • 1.2 案例2:饼图填充
  • 方法2:applyhatch函数
    • 2.1 案例1:柱状图填充
    • 2.2 案例2:饼图填充
  • 方法3: applyhatch_plusC函数
    • 3.1 案例1:柱状图填充
    • 3.2 案例2:饼图填充
  • 参考

带填充纹理的堆叠图是通过在原始堆叠图的基础上添加不同的纹理得到的,可以很好地解决由于 颜色区分不够而导致的对象识别困难问题。

由于Matlab中未收录提供填充纹理选项,因此需要大家自行设法解决。本博客介绍三种填充方法。

方法1:hatchfill2工具

MATLAB官网-Hatchfill2(Kesh Ikuma. Matlab Central, 2023)
在这里插入图片描述

1.1 案例1:柱状图填充

成图如下:
在这里插入图片描述
MATLAB代码如下:

clc
close all
clear
%% 基本设置
pathFigure= '.\Figures\' ;

%% Example 1:柱状图填充

figure(1);
h = bar(rand(3,4));
xlabel('Xlabel','fontsize',14,'FontName','Times New Roman','FontWeight','Bold')
ylabel('Ylabel','fontsize',14,'FontName','Times New Roman','FontWeight','Bold')
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');

str= strcat(pathFigure, "Figure1", '.tiff');
print(gcf, '-dtiff', '-r600', str);

figure(2);
hp = bar(rand(3,4));
xlabel('Xlabel','fontsize',14,'FontName','Times New Roman','FontWeight','Bold')
ylabel('Ylabel','fontsize',14,'FontName','Times New Roman','FontWeight','Bold')
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');
hatchfill2(hp(1),'single','HatchAngle',0);
hatchfill2(hp(2),'cross','HatchAngle',45);
hatchfill2(hp(3),'single','HatchAngle',90);

str= strcat(pathFigure, "Figure2", '.tiff');
print(gcf, '-dtiff', '-r600', str);

此外,也可以尝试黑白配色(FaceColor设置为白色即可):

1.2 案例2:饼图填充

成图如下:
在这里插入图片描述
MATLAB代码如下:

clc
close all
clear
%% 基本设置
pathFigure= '.\Figures\' ;

%% Example 2:饼图填充

figure(3);
colormap(cool(4));
h = pie(rand(4,1));
th = findobj(gca, 'Type', 'text');
set(th, 'FontName', 'Times New Roman', 'FontSize', 12)
hl = legend('Jan','Feb','Mar','Apr','May','Jun');
set(hl,'Box','off','Location','southOutside','NumColumns',3);
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');

str= strcat(pathFigure, "Figure3", '.tiff');
print(gcf, '-dtiff', '-r600', str);

figure(4);
colormap(cool(4));
hp = pie(rand(4,1));
hatchfill2(hp(1,1),'single','HatchAngle',0);
hatchfill2(hp(1,3),'cross','HatchAngle',45);
hatchfill2(hp(1,5),'single','HatchAngle',60);
hatchfill2(hp(1,7),'single','HatchAngle',90);
th = findobj(gca, 'Type', 'text');
set(th, 'FontName', 'Times New Roman', 'FontSize', 12)
hl = legend(hp(1, [1,3,5,7]),'Jan','Feb','Mar','Apr');
set(hl,'Box','off','Location','southOutside','NumColumns',3);
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');

str= strcat(pathFigure, "Figure4", '.tiff');
print(gcf, '-dtiff', '-r600', str);

方法2:applyhatch函数

Documentation of applyhatch

调用函数如下:

function applyhatch(h,patterns,colorlist)
% APPLYHATCH Apply 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

%  Copyright 2002-2009 The MathWorks, Inc.
  
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
if verLessThan('matlab','8.4.0')
  bits = hardcopy(h,'-dzbuffer','-r0');
else
  bits = print(h,'-RGBImage','-r0');
end
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');
end

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

end


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

%  Copyright 2002-2009 The MathWorks, Inc.

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

end

2.1 案例1:柱状图填充

成图如下:
在这里插入图片描述
MATLAB代码如下:

clc
close all
clear
%% 基本设置
pathFigure= '.\Figures\' ;


%% Example 1:

figure(1);
h = bar(rand(3,4));
xlabel('Xlabel','fontsize',14,'FontName','Times New Roman','FontWeight','Bold')
ylabel('Ylabel','fontsize',14,'FontName','Times New Roman','FontWeight','Bold')
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');
str= strcat(pathFigure, "Figure1", '.tiff');
print(gcf, '-dtiff', '-r600', str);

applyhatch(gcf,'\-x.');
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');

str= strcat(pathFigure, "Figure2", '.tiff');
print(gcf, '-dtiff', '-r600', str);

2.2 案例2:饼图填充

成图如下:
在这里插入图片描述
MATLAB代码如下:

clc
close all
clear
%% 基本设置
pathFigure= '.\Figures\' ;

%% Example 2:

figure(3);
colormap(cool(4));
h = pie(rand(4,1));
th = findobj(gca, 'Type', 'text');
set(th, 'FontName', 'Times New Roman', 'FontSize', 12)
hl = legend('Jan','Feb','Mar','Apr','May','Jun');
set(hl,'Box','off','Location','southOutside','NumColumns',3);
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');

str= strcat(pathFigure, "Figure3", '.tiff');
print(gcf, '-dtiff', '-r600', str);

applyhatch(gcf,'|-+.', cool(4));
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');


str= strcat(pathFigure, "Figure4", '.tiff');
print(gcf, '-dtiff', '-r600', str);

方法3: applyhatch_plusC函数

MATLAB官网-applyhatch_plusC函数
在这里插入图片描述
但是问题来了,在较新版本(如R2019a)的matlab中将applyhatch函数中用到的hardcopy函数去掉了,会提示 “未定义函数或变量 ‘hardcopy’”错误。

一个简单的解决方案是将以下代码进行替换

bits = hardcopy(h,'-dzbuffer',['-r' num2str(dpi)]);
bits = print('-RGBImage');

但是最后的结果很差,图像失真很严重,并且不能调整。因此并不建议用此函数。

3.1 案例1:柱状图填充

成图如下:
在这里插入图片描述
MATLAB代码如下:

clc
close all
clear
%% 基本设置
pathFigure= '.\Figures\' ;

% 图片尺寸设置(单位:厘米)
% ----------------------------------------------
figureUnits = 'centimeters';
figureWidth = 35;
figureHeight = 30;


%% Example 1:柱状图填充

figureHandle = figure;
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]); % define the new figure dimensions
h = bar(rand(3,4));
xlabel('Xlabel','fontsize',14,'FontName','Times New Roman','FontWeight','Bold')
ylabel('Ylabel','fontsize',14,'FontName','Times New Roman','FontWeight','Bold')
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');

str= strcat(pathFigure, "Figure1", '.tiff');
print(gcf, '-dtiff', '-r600', str);


[im_hatch1,colorlist] = applyhatch_pluscolor(gcf,'\-x.',0,0,[],150);

str= strcat(pathFigure, "Figure2", '.tiff');
print(gcf, '-dtiff', '-r600', str);

3.2 案例2:饼图填充

成图如下:
在这里插入图片描述
MATLAB代码如下:

clc
close all
clear
%% 基本设置
pathFigure= '.\Figures\' ;

% 图片尺寸设置(单位:厘米)
% ----------------------------------------------
figureUnits = 'centimeters';
figureWidth = 35;
figureHeight = 30;
%% Example 2:饼图填充

figureHandle = figure;
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]); % define the new figure dimensions
colormap(cool(6));
h = pie(rand(6,1));
th = findobj(gca, 'Type', 'text');
set(th, 'FontName', 'Times New Roman', 'FontSize', 12)
hl = legend('Jan','Feb','Mar','Apr','May','Jun');
set(hl,'Box','off','Location','southOutside','NumColumns',3);
set(gca,'Layer','top','FontSize',14,'Fontname', 'Times New Roman');

str= strcat(pathFigure, "Figure3", '.tiff');
print(gcf, '-dtiff', '-r600', str);


im_hatch2 = applyhatch_pluscolor(gcf,'|-.+\/',1,[1 1 0 1 0 0],cool(6),200,3,2);


str= strcat(pathFigure, "Figure4", '.tiff');
print(gcf, '-dtiff', '-r600', str);

参考

1.CSDN博客-matlab画柱状图并填充
2.CSDN博客-matlab画条纹填充(Hatched Fill)图 填坑 applyhatch hardcopy

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/433351.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

分析软件及其隐藏后门实验笔记

软件后门和软件加壳是什么 软件后门可以理解为在软件中植入病毒等具有后门功能的代码&#xff0c;通过运行软件来对用户的系统造成破坏、窃取机密等。 软件加壳一种常用的方式是在二进制的程序中植入一段代码&#xff0c;在运行的时候优先取得程序的控制权&#xff0c;之后再把…

Cisco SD-WAN (Viptela) version 20.11.1 ED - 软件定义广域网

请访问原文链接&#xff1a;https://sysin.org/blog/cisco-sd-wan-20/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org 支持 SASE 的架构&#xff0c;其集成了面向多云、安全、统一通信和应用优化的各种功能&#xff0c;可用于轻…

ChatGPT干掉程序员?想多了...

GPT-4才诞生没几天&#xff0c;感觉朋友圈已经被这个人工智能刷屏了&#xff0c;大家一边在感叹人工智能行业蓬勃发展的同时&#xff0c;一边又有不少人患上了AI焦虑症。 这其中&#xff0c;以程序员首当其冲。原因无他&#xff0c;只因为GPT-4的惊人的能力和不少大佬的发言。 …

4.17~4.18学习总结

网络编程 概述 1.什么是网络编程 在网络通信协议下&#xff0c;不同计算机上运行的程序&#xff0c;进行的数据传输&#xff0c;计算机跟计算机之间可以通过网络进行数据传输。 2.常见的软件架构&#xff1a; B/S&#xff0c;C/S 3.通信的软件架构CS BS各有什么区别和优点…

阿里云免费使用stable diffusion三个月【ai生成图片】详细教程【保姆级】

起因 这两天关注了ai生成图片&#xff0c;尝试了mijiourney服务【比较贵没入手】&#xff0c;结果免费的没有了&#xff0c;没用上&#xff0c;换了国内的一些小程序体验了下 综合体验式是太慢了&#xff0c;而他们是基于国外开源的stable diffiusion模型开发的【可以比肩mij…

【FAQ】关于华为推送服务因营销消息频次管控导致服务通讯类消息下发失败的解决方案

一&#xff0e; 问题描述 使用华为推送服务下发IM消息时&#xff0c;下发消息请求成功且code码为80000000&#xff0c;但是手机总是收不到消息&#xff1b; 在华为推送自助分析&#xff08;Beta&#xff09;平台查看发现&#xff0c;消息发送触发了频控。 二&#xff0e; 问题…

java 快排算法详解,java 快排代码

快排是一种高效的数据结构&#xff0c;它使用一个关键字&#xff08;Key&#xff09;来表示数据元素的一个集合。也就是说&#xff0c;快排是一个有序数组&#xff0c;而这个有序数组由两个元素组成。 快排的基本思想是&#xff1a;如果数组元素的值比它前面的两个元素都大&…

记录一 :对象锁和类锁

目录 简介 通过8个案例来解释说明 案例及总结 简介 阿里规约【强制】高并发时&#xff0c;同步调用应该去考量锁的性能损耗。能用无锁数据结构&#xff0c;就不要用锁&#xff1b;能 锁区块&#xff0c;就不要锁整个方法体&#xff1b;能用对象锁&#xff0c;就不要用类锁。…

提高工作效率的宝藏网站和宝藏工具

一、好用的网站 面包多 面包多 创作者在面包多&#xff0c;通过出售课程&#xff0c;文章&#xff0c;绘画&#xff0c;创意作品&#xff0c;软件&#xff0c;电子书&#xff0c;音乐&#xff0c; 游戏&#xff0c;咨询服务&#xff0c;每月获得 数百万元 收入。 写作素材模板…

二阶段算法:R-CNN类网络

博主简介 博主是一名大二学生&#xff0c;主攻人工智能研究。感谢让我们在CSDN相遇&#xff0c;博主致力于在这里分享关于人工智能&#xff0c;c&#xff0c;Python&#xff0c;爬虫等方面知识的分享。 如果有需要的小伙伴可以关注博主&#xff0c;博主会继续更新的&#xff0c…

【Java 数据结构】ArrayList的实现和底层源码讲解

&#x1f389;&#x1f389;&#x1f389;点进来你就是我的人了 博主主页&#xff1a;&#x1f648;&#x1f648;&#x1f648;戳一戳,欢迎大佬指点!人生格言&#xff1a;当你的才华撑不起你的野心的时候,你就应该静下心来学习! 欢迎志同道合的朋友一起加油喔&#x1f9be;&am…

java day9

第九章 使用swing 9.1 创建应用程序9.1.1 创建页面9.1.2 开发框架9.1.3 创建组件&& 9.1.4 将组件加入到容器中 9.2 使用组件9.2.1 图标9.2.2 标签9.2.3 文本框9.2.4 文本区域9.2.5 可滚动窗格9.2.6 复选框和单选按钮9.2.7 组合框9.2.8 列表 9.1 创建应用程序 import j…

FPGA基于SFP光口实现10G万兆网UDP通信 10G Ethernet Subsystem替代网络PHY芯片 提供工程源码和技术支持

目录 1、前言2、我这里已有的UDP方案3、详细设计方案4、vivado工程详解5、上板调试验证并演示6、福利&#xff1a;工程代码的获取 1、前言 目前网上的fpga实现udp基本生态如下&#xff1a; 1&#xff1a;verilog编写的udp收发器&#xff0c;但不带ping功能&#xff0c;这样的代…

The GNU nano text editor (文本编辑器)

The GNU nano text editor (文本编辑器) https://www.nano-editor.org/ GNU nano is a small and friendly text editor. 1 GNU nano The GNU nano text editor https://www.nano-editor.org/dist/latest/nano.html Source Code https://git.savannah.gnu.org/cgit/nano.gi…

EIGRP 配置,详解拓扑表,路由汇聚

1.3 EIGRP 拓扑&#xff0c;路由以及汇聚 1.3.1 实验目的 通过对 EIGRP 拓扑&#xff0c;路由以及汇聚相关实验的练习&#xff0c;掌握 EIGRP 建立拓扑信息的方式&#xff0c; 度量计算方法&#xff0c;如何调整度量&#xff0c;非等价负载均衡&#xff0c;以及 EIGRP 末节路…

anaconda ( jupyter notebook ) 安装 Cartopy库

文章目录 一、Cartopy库是什么&#xff1f;二、一步到位安装&#xff08;装不上的话用下面那个方法虚拟环境安装&#xff09;三、如何在anaconda ( jupyter notebook ) 虚拟环境安装 Cartopy库&#xff1f; 一、Cartopy库是什么&#xff1f; Cartopy 是一个开源免费的第三方 P…

mac压缩文件多了__MACOSX目录问题

文章目录 背景原因解决方案&#xff1a;更换压缩方式分析问题拓展&#xff08;.DS_Store&#xff09; 背景 项目中有一个场景&#xff0c;需要把目录压缩为app离线包的zip 但是压缩之后一致打不开&#xff0c;别人上传的zip是好的 原因 如图&#xff0c;我上传的在安卓设备…

D.8零样本文本分类应用:基于UTC的医疗意图分类,打通数据标注-模型训练-模型调优-预测部署全流程。

NLP专栏简介:数据增强、智能标注、意图识别算法|多分类算法、文本信息抽取、多模态信息抽取、可解释性分析、性能调优、模型压缩算法等 专栏详细介绍:NLP专栏简介:数据增强、智能标注、意图识别算法|多分类算法、文本信息抽取、多模态信息抽取、可解释性分析、性能调优、模型…

如何制作 ChatGPT 清晰有效咒语与Chat GPT高效交流——基础篇 第二课

在上一篇文章中&#xff0c;我们已经了解了 ChatGPT 的特性、应用范围以及逆天之处。然而&#xff0c;要想获得 ChatGPT 的逆天能力&#xff0c;最关键的一点就是必须掌握准确的“咒语”&#xff0c;即让其能够准确地理解我们所说的话&#xff0c;以及我们想要的东西。本篇文章…

一条记录的多幅面孔-事务的隔离级别与 MVCC

一、事务隔离级别 引出&#xff1a;**事务的隔离性要求&#xff0c;**理论上在某个事务对某个数据进行访问时&#xff0c;其他事务应该进行排队&#xff0c;当该事务提交之后&#xff0c;其他事务才可以继续访问这个数据。我们既想保持事务的 隔离性 &#xff0c;又想让服务器…