国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【MATLAB基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖

這篇具有很好參考價(jià)值的文章主要介紹了【MATLAB基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。


帶填充紋理的堆疊圖是通過在原始堆疊圖的基礎(chǔ)上添加不同的紋理得到的,可以很好地解決由于 顏色區(qū)分不夠而導(dǎo)致的對象識別困難問題。

由于Matlab中未收錄提供填充紋理選項(xiàng),因此需要大家自行設(shè)法解決。本博客介紹三種填充方法。

方法1:hatchfill2工具

MATLAB官網(wǎng)-Hatchfill2(Kesh Ikuma. Matlab Central, 2023)
【MATLAB基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖

1.1 案例1:柱狀圖填充

成圖如下:
【MATLAB基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖
MATLAB代碼如下:

clc
close all
clear
%% 基本設(shè)置
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設(shè)置為白色即可):

1.2 案例2:餅圖填充

成圖如下:
【MATLAB基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖
MATLAB代碼如下:

clc
close all
clear
%% 基本設(shè)置
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函數(shù)

Documentation of applyhatch

調(diào)用函數(shù)如下:

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基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖
MATLAB代碼如下:

clc
close all
clear
%% 基本設(shè)置
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基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖
MATLAB代碼如下:

clc
close all
clear
%% 基本設(shè)置
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函數(shù)

MATLAB官網(wǎng)-applyhatch_plusC函數(shù)
【MATLAB基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖
但是問題來了,在較新版本(如R2019a)的matlab中將applyhatch函數(shù)中用到的hardcopy函數(shù)去掉了,會(huì)提示 “未定義函數(shù)或變量 ‘hardcopy’”錯(cuò)誤。

一個(gè)簡單的解決方案是將以下代碼進(jìn)行替換

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

但是最后的結(jié)果很差,圖像失真很嚴(yán)重,并且不能調(diào)整。因此并不建議用此函數(shù)。

3.1 案例1:柱狀圖填充

成圖如下:
【MATLAB基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖
MATLAB代碼如下:

clc
close all
clear
%% 基本設(shè)置
pathFigure= '.\Figures\' ;

% 圖片尺寸設(shè)置(單位:厘米)
% ----------------------------------------------
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基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖
MATLAB代碼如下:

clc
close all
clear
%% 基本設(shè)置
pathFigure= '.\Figures\' ;

% 圖片尺寸設(shè)置(單位:厘米)
% ----------------------------------------------
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.zghlxwxcb.cn/news/detail-438710.html

到了這里,關(guān)于【MATLAB基礎(chǔ)繪圖第2棒】繪制柱狀/餅圖填充圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【MATLAB基礎(chǔ)繪圖第5棒】繪制氣泡圖

    【MATLAB基礎(chǔ)繪圖第5棒】繪制氣泡圖

    MATLAB繪制氣泡圖可用來對比不同數(shù)據(jù)組數(shù)據(jù)、評估指標(biāo)權(quán)重可視化等,需基于MATLAB2021a及以上版本進(jìn)行繪制,該2021a之前版本無氣泡圖函數(shù)。 以評估指標(biāo)權(quán)重可視化為例,AHP法經(jīng)專家賦權(quán)后獲得的判斷矩陣如圖1,現(xiàn)利用氣泡圖來將其可視化,更直觀的給審稿人印象,繪制步驟

    2024年02月14日
    瀏覽(24)
  • R語言繪圖:繪制橫向柱狀圖

    R語言繪圖:繪制橫向柱狀圖

    代碼主要實(shí)現(xiàn): 對數(shù)據(jù)進(jìn)行排序,并且相同分組的數(shù)據(jù)會(huì)有相同的顏色。最后,繪制橫向柱狀圖。 結(jié)果展示:

    2024年04月16日
    瀏覽(21)
  • 微信小程序Canvas繪制曲線圖餅圖柱狀圖雷達(dá)圖蛛網(wǎng)圖實(shí)現(xiàn)(附源碼)
  • 【Python數(shù)據(jù)可視化】matplotlib之繪制常用圖形:折線圖、柱狀圖(條形圖)、餅圖和直方圖

    【Python數(shù)據(jù)可視化】matplotlib之繪制常用圖形:折線圖、柱狀圖(條形圖)、餅圖和直方圖

    文章傳送門 Python 數(shù)據(jù)可視化 matplotlib之繪制常用圖形:折線圖、柱狀圖(條形圖)、餅圖和直方圖 matplotlib之設(shè)置坐標(biāo):添加坐標(biāo)軸名字、設(shè)置坐標(biāo)范圍、設(shè)置主次刻度、坐標(biāo)軸文字旋轉(zhuǎn)并標(biāo)出坐標(biāo)值 matplotlib之增加圖形內(nèi)容:設(shè)置圖例、設(shè)置中文標(biāo)題、設(shè)置網(wǎng)格效果 matplo

    2024年01月16日
    瀏覽(31)
  • 【MATLAB基礎(chǔ)繪圖第4棒】繪制橢圓形相關(guān)系矩陣圖

    【MATLAB基礎(chǔ)繪圖第4棒】繪制橢圓形相關(guān)系矩陣圖

    數(shù)據(jù)及代碼下載: 下載專區(qū)-《MATLAB統(tǒng)計(jì)分析與應(yīng)用:40個(gè)案例分析》程序與數(shù)據(jù) 繪圖函數(shù): 數(shù)據(jù)如下: MATLAB代碼如下: 運(yùn)行上述命令得出變量間的相關(guān)系數(shù)矩陣R、線性相關(guān)性檢驗(yàn)的p值矩陣P以及相關(guān)系數(shù)矩陣圖。成圖如下所示: 圖形參數(shù)修改可根據(jù)需要對 matrixplot函數(shù) 進(jìn)

    2024年02月04日
    瀏覽(21)
  • 【MATLAB基礎(chǔ)繪圖第9棒】繪制截?cái)嘧鴺?biāo)軸(Broken Axis)

    【MATLAB基礎(chǔ)繪圖第9棒】繪制截?cái)嘧鴺?biāo)軸(Broken Axis)

    有時(shí)候,用MATLAB繪制坐標(biāo)圖時(shí)會(huì)出現(xiàn)有的曲線值都特別大,有的曲線值都很小,但是又想在同一幅圖中將他們展示出來,于是需要截?cái)嘧鴺?biāo)軸的刻度或者改變縱軸的刻度,使其不均勻。此時(shí),就需要對橫坐標(biāo)或縱坐標(biāo)進(jìn)行截?cái)唷?參考:博客-MATLAB實(shí)例:截?cái)嘧鴺?biāo)軸(Broken Axis)

    2024年02月06日
    瀏覽(32)
  • python讀取excel數(shù)據(jù)并用雙y軸繪制柱狀圖和折線圖,柱子用漸變顏色填充

    python讀取excel數(shù)據(jù)并用雙y軸繪制柱狀圖和折線圖,柱子用漸變顏色填充

    往期python繪圖合集: python繪制簡單的折線圖 python讀取excel中數(shù)據(jù)并繪制多子圖多組圖在一張畫布上 python繪制帶誤差棒的柱狀圖 python繪制多子圖并單獨(dú)顯示 python讀取excel數(shù)據(jù)并繪制多y軸圖像 python繪制柱狀圖并美化|不同顏色填充柱子 python隨機(jī)生成數(shù)據(jù)并用雙y軸繪制兩條帶誤差

    2024年02月10日
    瀏覽(28)
  • Pearson相關(guān)性分析& plot繪圖(相關(guān)性系數(shù)柱狀圖、繪制非空值數(shù)量柱狀圖)

    Pearson相關(guān)性分析& plot繪圖(相關(guān)性系數(shù)柱狀圖、繪制非空值數(shù)量柱狀圖)

    Pearson相關(guān)性分析是一種用于檢測兩個(gè)變量之間線性關(guān)系強(qiáng)度的統(tǒng)計(jì)方法,其結(jié)果介于-1和1之間。一個(gè)相關(guān)系數(shù)為1表示完全正相關(guān),-1表示完全負(fù)相關(guān),0則表示沒有線性關(guān)系。 Pearson相關(guān)性分析假設(shè)數(shù)據(jù)來自正態(tài)分布,并且對異常值敏感。

    2024年02月09日
    瀏覽(24)
  • Matlab進(jìn)階繪圖第16期—三維填充折線圖

    Matlab進(jìn)階繪圖第16期—三維填充折線圖

    三維填充折線圖是在三維折線圖的基礎(chǔ)上,對其與XOY平面之間的部分進(jìn)行顏色填充,從而 能夠更好地刻畫細(xì)節(jié)變化 。 由于Matlab中未收錄三維填充折線圖的繪制函數(shù),因此需要大家自行設(shè)法解決 。 本文使用自制的FilledPlot3小工具進(jìn)行三維填充折線圖的繪制,先來看一下成品效

    2024年02月08日
    瀏覽(57)
  • MATLAB--pie函數(shù)繪制分類餅圖(1)--附案例代碼

    MATLAB--pie函數(shù)繪制分類餅圖(1)--附案例代碼

    MATLAB是一種功能強(qiáng)大的數(shù)學(xué)軟件,具備豐富的繪圖功能。在數(shù)據(jù)可視化中,分類圖是一種常用的方式,通過 pie 函數(shù),我們可以輕松創(chuàng)建美觀的分類餅圖。本文將介紹如何使用MATLAB的 pie 函數(shù)繪制分類圖,并提供一個(gè)簡單的案例,并附上案例代碼。 pie 函數(shù)用于創(chuàng)建餅圖,顯示

    2024年01月23日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包