声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 10422|回复: 3

[图像处理] 柱状图中怎么加斜线或者点状图例?

[复制链接]
发表于 2008-12-21 11:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
请问在matlab中怎么将柱状图的图例中的填充成斜线或者点状的呀?

我在matlab的Figure Properties中没找到怎么改呀!请大家指点一下了!
回复
分享到:

使用道具 举报

发表于 2008-12-21 12:42 | 显示全部楼层

回复 楼主 hitzk 的帖子

自己产生一个斜线或点状的图档 dot.tif, 再使用
[x,y,z]=cylinder; I=imread('dot.tif'); warp(x,y,z,I);
可以吗?!

评分

1

查看全部评分

发表于 2008-12-21 13:53 | 显示全部楼层

回复 楼主 hitzk 的帖子

这个问题好像是不能通过更改Figre Properties来实现的。下面的程序可以实现这个功能,不过是先按照图像处理的方法来做的。
主函数

  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.   else
  108.     colors = (colorlist(out,1) == bits(:,:,1)) & ...
  109.              (colorlist(out,2) == bits(:,:,2)) & ...
  110.              (colorlist(out,3) == bits(:,:,3));
  111.     return
  112.   end
  113. end
复制代码
需调用的函数

  1. function A = makehatch(hatch)
  2. %MAKEHATCH Predefined hatch patterns
  3. %  MAKEHATCH(HATCH) returns a matrix with the hatch pattern for HATCH
  4. %   according to the following table:
  5. %      HATCH        pattern
  6. %     -------      ---------
  7. %        /          right-slanted lines
  8. %        \          left-slanted lines
  9. %        |          vertical lines
  10. %        -          horizontal lines
  11. %        +          crossing vertical and horizontal lines
  12. %        x          criss-crossing lines
  13. %        .          single dots
  14. %
  15. %  See also: APPLYHATCH

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

  18. n = 6;
  19. A=zeros(n);
  20. switch (hatch)
  21. case '/'
  22.   A = fliplr(eye(n));
  23. case '\'
  24.   A = eye(n);
  25. case '|'
  26.   A(:,1) = 1;
  27. case '-'
  28.   A(1,:) = 1;
  29. case '+'
  30.   A(:,1) = 1;
  31.   A(1,:) = 1;
  32. case 'x'
  33.   A = eye(n) | fliplr(diag(ones(n-1,1),-1));
  34. case '.'
  35.   A(1:2,1:2)=1;
  36. otherwise
  37.   error(['Undefined hatch pattern "' hatch '".']);
  38. end

复制代码
这个就是注释中例子的效果图。
u1.jpg

评分

1

查看全部评分

发表于 2013-9-7 10:04 | 显示全部楼层

这个程序好像有问题吧,都运行出错
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-28 22:13 , Processed in 0.071674 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表