加载中…
个人资料
招展如桦
招展如桦
  • 博客等级:
  • 博客积分:0
  • 博客访问:6,463
  • 关注人气:184
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

MATLAB画柱状图填充(多种)(包括饼型图)

(2016-03-31 08:19:22)
标签:

mtalb

画图

分类: Code

from: http://blog.sina.com.cn/s/blog_6d5ffd0d0100osri.html

and From more .........

 

代码如下:

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

By Ben Hinkle, bhinkle@mathworks.com
This code is in the public domain. 
  
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
bits = hardcopy(h,'-dzbuffer','-r0');
set(h,'paperpositionmode',oldppmode);

bwidth = size(bits,2);
bheight = size(bits,1);
bsize = bwidth * bheight;

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');

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

% 而applyhatch函数需要调用下面的函数

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
               criss-crossing lines
               single dots
%
See also: APPLYHATCH

By Ben Hinkle, bhinkle@mathworks.com
This code is in the public domain.

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

测试代码如下:

data = [96.3,92.6,71.2;95.7,93.6,83.9;96.8,94.3,78.3;95.8,92.7,80.3]
 bar(data,1)
 axis([0 6 0.0 100])
 legend('方法','exited','Square')
 set(gca,'XTickLabel',{'Img1','Img2','Img3','Img4'})
 applyhatch(gcf,'\.x.')

结果如下:

http://img.ph.126.net/NZaQnbK5YXjP3ZZgHvL2JQ==/2854437738824391859.jpg

也就是,引入上面的函数,画,柱形图的时候加上这一句就可以了

 applyhatch(gcf,'\.x.')

实际上饼型图也可以,上面有个实例,我测试结果如下

http://img.ph.126.net/jVvdh8fZeyeoWuKqPnKUbw==/2854437738824391860.jpg

 语句复杂一点点

applyhatch(gcf,'|-+.\/',cool(6));

这段代码也有点生成的图片估计是固定大小的,这样,论文中用这些图片还是有麻烦的,不知道该怎么设置?

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有