hitzk 发表于 2008-12-21 11:14

柱状图中怎么加斜线或者点状图例?

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

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

ChaChing 发表于 2008-12-21 12:42

回复 楼主 hitzk 的帖子

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

sogooda 发表于 2008-12-21 13:53

回复 楼主 hitzk 的帖子

这个问题好像是不能通过更改Figre Properties来实现的。下面的程序可以实现这个功能,不过是先按照图像处理的方法来做的。
主函数
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;
if ~isempty(colorlist)
colorlist = uint8(255*colorlist);
= 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,);
if ratioh*pheight > bheight
    bigpattern(bheight+1:end,:) = [];
end
if ratiow*pwidth > bwidth
    bigpattern(:,bwidth+1:end) = [];
end
bigpattern = repmat(bigpattern,);
color = (bits(:,:,1) == colorval(1)) & ...
          (bits(:,:,2) == colorval(2)) & ...
          (bits(:,:,3) == colorval(3));
color = repmat(color,);
bits(color) = bigpattern(color);
if ~isempty(colorlist)
    = 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',);
set(imaxes,'position',,'visible','off');
set(newfig,'visible','on');

function = nextnonbw(ind,colorlist,bits)
out = ind+1;
colors = [];
while out <= size(colorlist,1)
if isequal(colorlist(out,:),) | ...
      isequal(colorlist(out,:),)
    out = out+1;
else
    colors = (colorlist(out,1) == bits(:,:,1)) & ...
             (colorlist(out,2) == bits(:,:,2)) & ...
             (colorlist(out,3) == bits(:,:,3));
    return
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

%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

这个就是注释中例子的效果图。

白云星空 发表于 2013-9-7 10:04

sogooda 发表于 2008-12-21 13:53 static/image/common/back.gif
这个问题好像是不能通过更改Figre Properties来实现的。下面的程序可以实现这个功能,不过是先按照图像处理 ...

这个程序好像有问题吧,都运行出错
页: [1]
查看完整版本: 柱状图中怎么加斜线或者点状图例?