首页 > 代码库 > Add hatch to bar plot

Add hatch to bar plot

 

  1 function applyhatch(h,patterns,colorlist)  2 %APPLYHATCH Apply hatched patterns to a figure  3 %  APPLYHATCH(H,PATTERNS) creates a new figure from the figure H by  4 %  replacing distinct colors in H with the black and white  5 %  patterns in PATTERNS. The format for PATTERNS can be  6 %    a string of the characters /, \‘, |, -, +, x, .  7 %    a cell array of matrices of zeros (white) and ones (black)  8 %  9 %  APPLYHATCH(H,PATTERNS,COLORS) maps the colors in the n by 3 10 %  matrix COLORS to PATTERNS. Each row of COLORS specifies an RGB 11 %  color value. 12 % 13 %  Note this function makes a bitmap image of H and so is limited 14 %  to low-resolution, bitmap output. 15 % 16 %  Example 1: 17 %    bar(rand(3,4)); 18 %    applyhatch(gcf,\-x.); 19 % 20 %  Example 2: 21 %    colormap(cool(6)); 22 %    pie(rand(6,1)); 23 %    legend(Jan,Feb,Mar,Apr,May,Jun); 24 %    applyhatch(gcf,|-+.\/,cool(6)); 25 % 26 %  See also: MAKEHATCH 27 %  By Ben Hinkle, bhinkle@mathworks.com 28 %  This code is in the public domain. 29   30 oldppmode = get(h,paperpositionmode); 31 oldunits = get(h,units); 32 set(h,paperpositionmode,auto); 33 set(h,units,pixels); 34 figsize = get(h,position); 35 if nargin == 2 36   colorlist = []; 37 end 38 bits = hardcopy(h,-dzbuffer,-r0); 39 set(h,paperpositionmode,oldppmode); 40 bwidth = size(bits,2); 41 bheight = size(bits,1); 42 bsize = bwidth * bheight; 43 if ~isempty(colorlist) 44   colorlist = uint8(255*colorlist); 45   [colors,colori] = nextnonbw(0,colorlist,bits); 46 else 47   colors = (bits(:,:,1) ~= bits(:,:,2)) | ... 48            (bits(:,:,1) ~= bits(:,:,3)); 49 end 50 pati = 1; 51 colorind = find(colors); 52 while ~isempty(colorind) 53   colorval(1) = bits(colorind(1)); 54   colorval(2) = bits(colorind(1)+bsize); 55   colorval(3) = bits(colorind(1)+2*bsize); 56   if iscell(patterns) 57     pattern = patterns{pati}; 58   elseif isa(patterns,char) 59     pattern = makehatch(patterns(pati)); 60   else 61     pattern = patterns; 62   end 63   pattern = uint8(255*(1-pattern)); 64   pheight = size(pattern,2); 65   pwidth = size(pattern,1); 66   ratioh = ceil(bheight/pheight); 67   ratiow = ceil(bwidth/pwidth); 68   bigpattern = repmat(pattern,[ratioh ratiow]); 69   if ratioh*pheight > bheight 70     bigpattern(bheight+1:end,:) = []; 71   end 72   if ratiow*pwidth > bwidth 73     bigpattern(:,bwidth+1:end) = []; 74   end 75   bigpattern = repmat(bigpattern,[1 1 3]); 76   color = (bits(:,:,1) == colorval(1)) & ... 77           (bits(:,:,2) == colorval(2)) & ... 78           (bits(:,:,3) == colorval(3)); 79   color = repmat(color,[1 1 3]); 80   bits(color) = bigpattern(color); 81   if ~isempty(colorlist) 82     [colors,colori] = nextnonbw(colori,colorlist,bits); 83   else 84     colors = (bits(:,:,1) ~= bits(:,:,2)) | ... 85              (bits(:,:,1) ~= bits(:,:,3)); 86   end 87   colorind = find(colors); 88   pati = (pati + 1); 89   if pati > length(patterns) 90     pati = 1; 91   end 92 end 93 newfig = figure(units,pixels,visible,off); 94 imaxes = axes(parent,newfig,units,pixels); 95 im = image(bits,parent,imaxes); 96 fpos = get(newfig,position); 97 set(newfig,position,[fpos(1:2) figsize(3) figsize(4)+1]); 98 set(imaxes,position,[0 0 figsize(3) figsize(4)+1],visible,off); 99 set(newfig,visible,on);100 function [colors,out] = nextnonbw(ind,colorlist,bits)101 out = ind+1;102 colors = [];103 while out <= size(colorlist,1)104   if isequal(colorlist(out,:),[255 255 255]) | ...105         isequal(colorlist(out,:),[0 0 0])106     out = out+1;107   else108     colors = (colorlist(out,1) == bits(:,:,1)) & ...109              (colorlist(out,2) == bits(:,:,2)) & ...110              (colorlist(out,3) == bits(:,:,3));111     return112   end113 end114 %而applyhatch函数需要调用下面的函数115 function A = makehatch(hatch)116 %MAKEHATCH Predefined hatch patterns117 %  MAKEHATCH(HATCH) returns a matrix with the hatch pattern for HATCH118 %   according to the following table:119 %      HATCH        pattern120 %     -------      ---------121 %        /          right-slanted lines122 %        \          left-slanted lines123 %        |          vertical lines124 %        -          horizontal lines125 %        +          crossing vertical and horizontal lines126 %        x          criss-crossing lines127 %        .          single dots128 %129 %  See also: APPLYHATCH130 %  By Ben Hinkle, bhinkle@mathworks.com131 %  This code is in the public domain.132 n = 6;133 A=zeros(n);134 switch (hatch)135 case /136   A = fliplr(eye(n));137 case \‘138   A = eye(n);139 case |140   A(:,1) = 1;141 case -142   A(1,:) = 1;143 case +144   A(:,1) = 1;145   A(1,:) = 1;146 case x147   A = eye(n) | fliplr(diag(ones(n-1,1),-1));148 case .149   A(1:2,1:2)=1;150 otherwise151   error([Undefined hatch pattern " hatch ".]);152 end153  154  155 %测试的例子命令156 %  data = http://www.mamicode.com/[96.3,92.6,71.2;95.7,93.6,83.9;96.8,94.3,78.3;95.8,92.7,80.3]157 %  bar(data,1)158 %  axis([0 6 0.0 100])159 %  legend(方法,exited,Square)160 %  set(gca,XTickLabel,{Img1,Img2,Img3,Img4})161 %  applyhatch(gcf,\.x.)

 

---恢复内容结束---

Add hatch to bar plot