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 == 2colorlist = [];
end
if verLessThan('matlab','8.4.0')bits = hardcopy(h,'-dzbuffer','-r0');
elsebits = 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);
elsecolors = (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));elsepattern = patterns;endpattern = 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 > bheightbigpattern(bheight+1:end,:) = [];endif ratiow*pwidth > bwidthbigpattern(:,bwidth+1:end) = [];endbigpattern = 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);elsecolors = (bits(:,:,1) ~= bits(:,:,2)) | ...(bits(:,:,1) ~= bits(:,:,3));endcolorind = find(colors);pati = (pati + 1);if pati > length(patterns)pati = 1;end
endnewfig = 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');
endfunction [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;elsecolors = (colorlist(out,1) == bits(:,:,1)) & ...(colorlist(out,2) == bits(:,:,2)) & ...(colorlist(out,3) == bits(:,:,3));returnend
endendfunction 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;otherwiseerror(['Undefined hatch pattern "' hatch '".']);
endend

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

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

  1. 【MATLAB基础绘图第10棒】绘制各种面积图

    MATLAB绘制面积图相关函数 1.1 area-填充区二维绘图 MATLAB中文帮助 语法: area(___,basevalue) basevalue 对应于水平基线 1.2 fill-创建二维填 ...

  2. 【MATLAB基础绘图第7棒】绘制各式柱状图

    MATLAB绘制各式柱状图 1 分组绘制柱状图 1.1 案例1:常规分组柱状图 2 绘制渐变柱状图 2.1 案例 3 绘制多组柱状图并修改文字颜色一致 3.1 案例 4 绘制双轴-倒立柱状图 5 绘制 ...

  3. 【MATLAB基础绘图第12棒】绘制饼状图

    MATLAB绘制饼状图 1 基础案例:修改颜色及字体 参考 1 基础案例:修改颜色及字体 成图如下: MATLAB代码如下: clc close all clear %% 导入数据 load('X.m ...

  4. label mpchart 饼图_Origin系列:绘制柱状堆积图

    原创不易,感谢分享,欢迎转发,请点在看 堆积柱状图十分美观,不仅能够展示数据占比,更能表现其变化趋势,是科研必备技能 今天分享粉丝提出类似下列图形用Origin绘制多列柱状堆积图.希望对大家有所帮助 ...

  5. 【Python基础绘图】Python多张png合成gif动图

    [Python基础绘图]Python多张png合成gif动图 前段时间准备答辩ppt想展示下逐月的空间变化趋势,故想到了动图的方式,将多张png图片转变为gif动图.下面是代码记录一下. 首先需要准备 ...

  6. echarts:饼、柱状、折线图的配置说明

    文章目录 前言 柱状图和折线图主要配置项: 饼图的基本配置项 series 数据项 有什么功能组件? 渐进式案例: vue中画一个基本的饼图 为它添加提示组件tooltip dataZom,用柱状图演 ...

  7. graphics | 基础绘图系统(八)——棘状图、符号图、多边形(路径)图、Cohen-Friendly关联图、条件密度图...

    本篇继续介绍基础绘图系统中的几个绘图函数. spineplot() 该函数用于绘制棘状图,有以下两种语法结构: spineplot(x, y = NULL,breaks = NULL, tol.yla ...

  8. 回归 统计绘图_手把手教绘制回归分析结果的森林图「GraphPad Prism和Excel」

    在之前的内容中,我们讲到通过展示森林图,可以更加直观的将回归分析结果可视化.详见:一文带你玩转森林图! 在本期内容中,我们来实际操练一下,介绍几款常用的软件,教大家绘制出高质量的森林图. 下面我们以2 ...

  9. graphics | 基础绘图系统(七)——各式各样的散点图/折线图

    前面已经用了六篇推文系统地介绍了R语言的基础绘图系统的主要函数用法,以及柱状图.直方图.箱形图.扇形图等常见图形的绘制方法,接下来将计划用四篇推文介绍使用基础绘图系统能够绘制的其他各类图形. 本篇介绍 ...

最新文章

  1. 读懂深度迁移学习,看这文就够了 | 赠书
  2. Win10 基于excel 文件的python下载脚本
  3. go语言连接redis(已测试)
  4. 【Linux系统编程】vfork() 函数详解
  5. HALCON示例程序gray_features.hdev提取灰度图的不同特征(area_center_gray 、elliptic_axis_gray)
  6. 前端学习(619):变量的小案例二
  7. html5证书,1+X证书Web前端开发HTML专项练习
  8. abp radio表单元素 消失了
  9. mysql主从复制的binlog和relay-log的区别
  10. 以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明
  11. 生物信息学分析的常用软件
  12. conda查看已装包_conda一个包管理器
  13. PDF拆分页面,免费拆分为多个PDF
  14. 人工智能机器视觉技术应用有哪些?
  15. Java中判断两个Date是否是同一天
  16. 连接服务器切换无线,怎么用路由器连接别人的wifi?
  17. Java GUI 表白程序 | 源码
  18. 如何沟通?如何商务谈判!《沃顿商学院最实用的谈判课》epub免费下载
  19. opencv背景抠图
  20. 八段数码管数字显示实验c语言,硬件实验十 八段数码管显示

热门文章

  1. 集成灶怎么选,市面上值得推荐的高性价比且质量好的集成灶甄选
  2. ABP框架-1.1 ABP总体介绍 - 入门介绍
  3. Linux系统下安装rz/sz命令及使用说明(详解)
  4. 苹果X可以升级5G吗_想换苹果X,但5G又快来了,到底要不要买才好?
  5. 取消中小学生各种艺术考级,家长们怎么看?
  6. window.localStorage.setItem的使用
  7. [Android Studio]详细讲解Android6.0以上请求应用权限(解决请求权限窗口一闪而过的问题)
  8. 啤酒瓶啤酒盖换啤酒_像啤酒中的免费一样导致心脏出血(甚至更多)
  9. 87-Java方法递归形式、算法流程总结、递归常见案例、递归经典案例-猴子吃桃问题、非规律化递归案例-文件搜索、啤酒问题
  10. 4500m a8 amd_a8 4500m cpu排行榜_...al AMD A8 4500M laptop CPU Quad Core A8-4500M 1.9-amd(2)