matlab画柱状图并填充
matlab画柱状图并填充
导读:记录一下如何使用matlab画柱状图,并进行填充。
版本:matlab 2017b.
注意:使用matlab2016版本似乎会有bug。
1.辅助函数makehatch.m
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;otherwiseerror(['Undefined hatch pattern "' hatch '".']);
end
2.辅助函数applyhatch.m
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');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;elsecolors = (colorlist(out,1) == bits(:,:,1)) & ...(colorlist(out,2) == bits(:,:,2)) & ...(colorlist(out,3) == bits(:,:,3));returnend
end
3.画柱状图的.m文件,其中调用了applyhatch()函数
%If you want to adjust the pattern to 6 bar such as " applyhatch(gcf,'.-+/|x');",
%try to type this "applyhatch(gcf,'.-++/||xx');" instedly.
%So you can avoid the duplicated pattern at least, even order problem is still not solved.
data=[345,359,209;155,161,99];%三列的柱状图,分为了两种,具体见图b=bar(data);%画柱状图text(0.73,345+12,'345');%设置柱状图上的数值大小
text(0.73+0.23,359+12,'359');
text(0.73+0.45,209+12,'209');text(0.73+1,155+12,'155');
text(0.73+1+0.23,161+12,'161');
text(0.73+1+0.46,99+12,'99');grid on;%添加网格线
%ch = get(b,'children');
title('实验2')
set(gca,'XTickLabel',{'LINEITEM','ORDERS'})%设置X轴显示%set(ch,'FaceVertexCData',[0 0 1;1 0 0;0 1 0])
legend('分区1','分区2','分区2');%设置
ylabel('时间(秒)');%设置y轴名字
applyhatch(gcf,'/\xx');%对原图添加填充,这里有个bug就是xx需要输入两次
显示结果:
填充后效果: