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

这篇具有很好参考价值的文章主要介绍了【MATLAB基础绘图第2棒】绘制柱状/饼图填充图。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


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

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

方法1:hatchfill2工具

MATLAB官网-Hatchfill2(Kesh Ikuma. Matlab Central, 2023)
【MATLAB基础绘图第2棒】绘制柱状/饼图填充图

1.1 案例1:柱状图填充

成图如下:
【MATLAB基础绘图第2棒】绘制柱状/饼图填充图
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基础绘图第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基础绘图第2棒】绘制柱状/饼图填充图
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基础绘图第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函数
【MATLAB基础绘图第2棒】绘制柱状/饼图填充图
但是问题来了,在较新版本(如R2019a)的matlab中将applyhatch函数中用到的hardcopy函数去掉了,会提示 “未定义函数或变量 ‘hardcopy’”错误。

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

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

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

3.1 案例1:柱状图填充

成图如下:
【MATLAB基础绘图第2棒】绘制柱状/饼图填充图
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基础绘图第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文章来源地址https://www.toymoban.com/news/detail-438710.html

到了这里,关于【MATLAB基础绘图第2棒】绘制柱状/饼图填充图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【MATLAB基础绘图第5棒】绘制气泡图

    MATLAB绘制气泡图可用来对比不同数据组数据、评估指标权重可视化等,需基于MATLAB2021a及以上版本进行绘制,该2021a之前版本无气泡图函数。 以评估指标权重可视化为例,AHP法经专家赋权后获得的判断矩阵如图1,现利用气泡图来将其可视化,更直观的给审稿人印象,绘制步骤

    2024年02月14日
    浏览(33)
  • R语言绘图:绘制横向柱状图

    代码主要实现: 对数据进行排序,并且相同分组的数据会有相同的颜色。最后,绘制横向柱状图。 结果展示:

    2024年04月16日
    浏览(24)
  • 【Python数据可视化】matplotlib之绘制常用图形:折线图、柱状图(条形图)、饼图和直方图

    文章传送门 Python 数据可视化 matplotlib之绘制常用图形:折线图、柱状图(条形图)、饼图和直方图 matplotlib之设置坐标:添加坐标轴名字、设置坐标范围、设置主次刻度、坐标轴文字旋转并标出坐标值 matplotlib之增加图形内容:设置图例、设置中文标题、设置网格效果 matplo

    2024年01月16日
    浏览(37)
  • 【MATLAB基础绘图第4棒】绘制椭圆形相关系矩阵图

    数据及代码下载: 下载专区-《MATLAB统计分析与应用:40个案例分析》程序与数据 绘图函数: 数据如下: MATLAB代码如下: 运行上述命令得出变量间的相关系数矩阵R、线性相关性检验的p值矩阵P以及相关系数矩阵图。成图如下所示: 图形参数修改可根据需要对 matrixplot函数 进

    2024年02月04日
    浏览(53)
  • 【MATLAB基础绘图第9棒】绘制截断坐标轴(Broken Axis)

    有时候,用MATLAB绘制坐标图时会出现有的曲线值都特别大,有的曲线值都很小,但是又想在同一幅图中将他们展示出来,于是需要截断坐标轴的刻度或者改变纵轴的刻度,使其不均匀。此时,就需要对横坐标或纵坐标进行截断。 参考:博客-MATLAB实例:截断坐标轴(Broken Axis)

    2024年02月06日
    浏览(35)
  • python读取excel数据并用双y轴绘制柱状图和折线图,柱子用渐变颜色填充

    往期python绘图合集: python绘制简单的折线图 python读取excel中数据并绘制多子图多组图在一张画布上 python绘制带误差棒的柱状图 python绘制多子图并单独显示 python读取excel数据并绘制多y轴图像 python绘制柱状图并美化|不同颜色填充柱子 python随机生成数据并用双y轴绘制两条带误差

    2024年02月10日
    浏览(35)
  • Pearson相关性分析& plot绘图(相关性系数柱状图、绘制非空值数量柱状图)

    Pearson相关性分析是一种用于检测两个变量之间线性关系强度的统计方法,其结果介于-1和1之间。一个相关系数为1表示完全正相关,-1表示完全负相关,0则表示没有线性关系。 Pearson相关性分析假设数据来自正态分布,并且对异常值敏感。

    2024年02月09日
    浏览(28)
  • Matlab进阶绘图第16期—三维填充折线图

    三维填充折线图是在三维折线图的基础上,对其与XOY平面之间的部分进行颜色填充,从而 能够更好地刻画细节变化 。 由于Matlab中未收录三维填充折线图的绘制函数,因此需要大家自行设法解决 。 本文使用自制的FilledPlot3小工具进行三维填充折线图的绘制,先来看一下成品效

    2024年02月08日
    浏览(58)
  • MATLAB--pie函数绘制分类饼图(1)--附案例代码

    MATLAB是一种功能强大的数学软件,具备丰富的绘图功能。在数据可视化中,分类图是一种常用的方式,通过 pie 函数,我们可以轻松创建美观的分类饼图。本文将介绍如何使用MATLAB的 pie 函数绘制分类图,并提供一个简单的案例,并附上案例代码。 pie 函数用于创建饼图,显示

    2024年01月23日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包