MATLAB 条形图或饼状图 图案填充_aizhao3648的博客-程序员秘密

技术标签: matlab  

function [im_hatch,colorlist] = applyhatch_pluscolor(h,patterns,CvBW,Hinvert,colorlist, ...
                                                dpi,hatchsc,lw)
%APPLYHATCH_PLUSCOLOR Apply hatched patterns to a figure in BW or Color
%  APPLYHATCH_PLUSCOLOR(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', '.', 'c', 'w', 'k'
%    (see makehatch_plus.m for more details) or
%    a cell array of matrices of zeros (white) and ones (black)
%
%  In addition, H can alternatively be a uint8 NxMx3 matrix of the type
%  produced by imread.  In this case, colors in this image will be
%  replaced with patterns as if it was a figure.  A final figure window
%  will be generated that displays the result.  The DPI argument
%  discussed below will be ignored if H is an image matrix.
%
%  APPLYHATCH_PLUSCOLOR(H,PATTERNS,CVBW) binary value for choice of Color or Black
%  and White plots. If color is chosen the color will match that of the 
%  current fill. 1 -> Color, anything else -> BW
%
%  APPLYHATCH_PLUSCOLOR(H,PATTERNS,CVBW,HINVERT) binary value to invert the hatch.
%  i.e., if it is black lines with a white background, that becomes white
%  lines with a black background. This can either be a scalar value or a 
%  1xN array equal to the length of PATTERNS. When used as an array each
%  PATTERNS(i) will be inverted according to Hinvert(i). 1 -> Invert,
%  anything else -> Non Inverted
%
%  APPLYHATCH_PLUSCOLOR(H,PATTERNS,CVBW,HINVERT,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 bitmap output.
%
%  Additional arguments:
%
%  [im_hatch,colorlist] = applyhatch_plus(h,patterns,CvBW,Hinvert,colorlist,dpi,hatchsc,linewidth)
%
%   input   DPI         allows specification of bitmap resolution, making plot resolution
%                       better for printing.  Ignored if H is an image matrix.
%           HATCHSC     multiplier for hatch scale to increase size of pattern for better operation
%                       at higher resolutions
%                       default [] uses screen resolution as in
%                       APPLYHATCH
%           LINEWIDTH   A scaling factor to apply to line and dot sizes
%                       in hatching.  Defaults to 1.
%   output  IM_HATCH    RGB bitmap matrix of new figure
%                       use IMWRITE to output in desired format
%           COLORLIST   List of colors actually replaced.  Useful info if
%                       no colorlist initially given to function.
%                       Colorlist will be uint8, not 0-1 scale as
%                       originally specified.
%
%  Example 1:
%    bar(rand(3,4));
%    [im_hatch,colorlist] = applyhatch_pluscolor(gcf,'\-x.',0,0,[],150);
%    imwrite(im_hatch,'im_hatch.png','png')
%
%  Example 2:
%    bar(rand(3,4));
%    [im_hatch,colorlist] = applyhatch_pluscolor(gcf,'\-x.',1,[],[],150);
%    imwrite(im_hatch,'im_hatch.png','png')
%
%  Example 3:
%    colormap(cool(6));
%    pie(rand(6,1));
%    legend('Jan','Feb','Mar','Apr','May','Jun');
%    im_hatch = applyhatch_pluscolor(gcf,'|-.+\/',1,[1 1 0 1 0 0],cool(6),200,3,2);
%    imwrite(im_hatch,'im_hatch.png','png')
%
%  Example 4: Produces roughly the same thing as example 1
%    bar(rand(3,4));
%    print -dtiff -r150 im.tiff
%    im = imread( 'im.tiff', 'tiff' );
%    [im_hatch,colorlist] = applyhatch_pluscolor(im,'\-x.');
%    imwrite(im_hatch,'im_hatch.tiff','tiff')
%    
%
% Modification of APPLYHATCH to allow higher resolution output
% Modified Brian FG Katz    8-aout-03
% Modified David M Kaplan   19-fevrier-08
%
% Modification of APPLYHATCH_PLUS to allow for color and inverted hatch
% Modified Brandon Levey  May 6, 2009
%
%  See also: APPLYHATCH, APPLYHATCH_PLUS, MAKEHATCH, MAKEHATCH_PLUS

%  By Ben Hinkle, [email protected]
%  This code is in the public domain. 


if ~exist('CvBW','var'); CvBW = 0      ; end  % defaults to black and white
if isempty(CvBW); CvBW = 0     ; end  % defaults to black and white
if (CvBW ~= 0 && CvBW ~= 1); CvBW = 0     ; end  % defaults to black and white

if ~exist('Hinvert','var'); Hinvert = 0      ; end  % defaults to not inverted
if isempty(Hinvert); Hinvert = 0     ; end  % defaults to not inverted
if length(Hinvert) == length(patterns) || length(Hinvert) == 1
    for i = 1:length(Hinvert)
        if Hinvert(i) ~= 0 && Hinvert(i) ~= 1; Hinvert(i) = 0     ; end
    end
else
    error(['The length of Hinvert must be 1 or equal to the length of PATTERNS']);
end

if ~exist('hatchsc','var'); hatchsc = 1      ; end
if ~exist('dpi','var'); dpi = 0          ; end     % defaults to screen resolution
if ~exist('colorlist','var'); colorlist = []   ; end
if ~exist('lw','var'); lw=1; end

if numel(h) == 1 % Assume it is a figure window
  oldppmode = get(h,'paperpositionmode');
  oldunits = get(h,'units');
  oldcolor = get(h,'color');
  oldpos = get(h,'position');
  set(h,'paperpositionmode','auto');
  set(h,'units','pixels');
  set(h,'color',[1 1 1]);
  figsize = get(h,'position');

  bits = hardcopy(h,'-dzbuffer',['-r' num2str(dpi)]);

  % % Try a different approach using a temporary file - use this if having probs
  % tn = [ tempname '.tif' ];
  % print( '-dtiff', [ '-r' num2str(dpi) ], tn )
  % bits = uint8( imread( tn, 'TIFF' ) );
  % delete(tn)
  
  set(h,'paperpositionmode',oldppmode);
  set(h,'color',oldcolor);
elseif size(h,3) == 3 % Assume it is an image matrix
  bits = h;
  oldunits='pixels';
  oldpos = [ 0, 0, size(bits,2), size(bits,1) ];
  figsize = oldpos;
else 
  error( 'Bad first argument.' );
end

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The next bit basically modernizes the original
% version of this function using things like unique
% and for loops
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make bitmap one long matrix with 3 columns
bits = reshape(bits,[bsize,3]);

% Convert original color scale to 255 scale
if ~isempty(colorlist)
  % NOTE: Added "floor" below because this seems to better pick out
  % correct colors produced by "hardcopy above better than uint8 by itself
  
  %colorlist = uint8(255*colorlist);
  colorlist = uint8(floor(255*colorlist));
else
  % Find unique colors in image - this takes a long time at high resolution
  [B,I,J] = unique( bits, 'rows' );

  switch CvBW
      case 0 % BW plot
        % Find just "colored" colors
        C = find( B(:,1)~=B(:,2) | B(:,1)~=B(:,3) );
      case 1 % color plot
        % Find all non black and white
        B = sortrows(B);
        C = 1:size(B,1);
        C = C(2:end-1)';
  end
  
  colorlist = B( C , : );
end

% Loop over list of colors and find matches
for k = 1:size(colorlist,1)

  % Find points that match color
  if exist('B','var') % Use unique colors if around
    I = C(k) == J;
  else % Otherwise test each point
    cc = colorlist(k,:);
    I = bits(:,1)==cc(1) & bits(:,2)==cc(2) & bits(:,3)==cc(3);
    if ~any(I(:)), continue, end
  end

  % What pattern to use
  pati = mod( k-1, numel(patterns) ) + 1;
  if iscell(patterns)
    pattern = patterns{pati};
  elseif isa(patterns,'char')
    pattern = makehatch_plus(patterns(pati),6*hatchsc,lw);
  else
    pattern = patterns;
  end
  pattern = uint8(1-pattern);
  
  if length(Hinvert) == 1
    invertHatch = logical(Hinvert);
  else
    invertHatch = logical(Hinvert(pati));
  end

  % Make a big pattern matching size of bits
  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
  
  % Put that pattern into bits and logical values based on CvBW and Hinvert
  switch CvBW
      case 0 % BW
          if invertHatch
              bits(find(I),:) = repmat(~bigpattern(I)*255,[1,3]);
          else
              bits(find(I),:) = repmat(bigpattern(I)*255,[1,3]);
          end
      case 1 % Color
          if invertHatch
              bits(find(I),:) = [ ...
                (uint8(bigpattern(I)) * colorlist(k,1)) + uint8((~bigpattern(I)) * 255), ...
                (uint8(bigpattern(I)) * colorlist(k,2)) + uint8((~bigpattern(I)) * 255), ...
                (uint8(bigpattern(I)) * colorlist(k,3)) + uint8((~bigpattern(I)) * 255)];
          else
              bits(find(I),:) = [ ...
                (uint8(~bigpattern(I)) * colorlist(k,1)) + uint8((bigpattern(I)) * 255), ...
                (uint8(~bigpattern(I)) * colorlist(k,2)) + uint8((bigpattern(I)) * 255), ...
                (uint8(~bigpattern(I)) * colorlist(k,3)) + uint8((bigpattern(I)) * 255)];
          end
  end
  
end

% Put bits back into its normal shape
bits = reshape( bits, [bheight,bwidth,3] );

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Replot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
newfig = figure('units',oldunits,'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(newfig,'position',oldpos)
set(newfig,'units','pixels')
set(imaxes,'position',[0 0 figsize(3) figsize(4)+1],'visible','off');
set(newfig,'visible','on');

set(newfig,'units','normalized');
set(imaxes,'units','normalized');
set(imaxes,'DataAspectRatio',[1 1 1],'DataAspectRatioMode','manual');


if nargout > 0, im_hatch = bits; end
if nargout < 2, clear colorlist; end

 

function A = makehatch_plus(hatch,n,m)
%MAKEHATCH_PLUS Predefined hatch patterns
%
% Modification of MAKEHATCH to allow for selection of matrix size. Useful whe using 
%   APPLYHATCH_PLUS with higher resolution output.
%
% input (optional) N    size of hatch matrix (default = 6)
% input (optional) M    width of lines and dots in hatching (default = 1)
%
%  MAKEHATCH_PLUS(HATCH,N,M) 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
%        .          square dots
%        c          circular dots
%        w          Just a blank white pattern
%        k          Just a totally black pattern
%
%  See also: APPLYHATCH, APPLYHATCH_PLUS, APPLYHATCH_PLUSCOLOR, MAKEHATCH

%  By Ben Hinkle, [email protected]
%  This code is in the public domain. 

% Modified Brian FG Katz    8-aout-03
% Modified David M Kaplan    19-fevrier-08

if ~exist('n','var'), n = 6; end
if ~exist('m','var'), m = 1; end
n=round(n);

switch (hatch)
  case '\'
    [B,C] = meshgrid( 0:n-1 );
    B = B-C; 
    clear C
    A = abs(B) <= m/2;
    A = A | abs(B-n) <= m/2;
    A = A | abs(B+n) <= m/2;
  case '/'
    A = fliplr(makehatch_plus('\',n,m));
  case '|'
    A=zeros(n);
    A(:,1:m) = 1;
  case '-'
    A = makehatch_plus('|',n,m);
    A = A';
  case '+'
    A = makehatch_plus('|',n,m);
    A = A | A';
  case 'x'
    A = makehatch_plus('\',n,m);
    A = A | fliplr(A);
  case '.'
    A=zeros(n);
    A(1:2*m,1:2*m)=1;
  case 'c'
    [B,C] = meshgrid( 0:n-1 );
    A = sqrt(B.^2+C.^2) <= m;
    A = A | fliplr(A) | flipud(A) | flipud(fliplr(A));
  case 'w'
    A = zeros(n);
  case 'k'
    A = ones(n);
  otherwise
    error(['Undefined hatch pattern "' hatch '".']);
end

 

转载于:https://www.cnblogs.com/wt869054461/p/9010670.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/aizhao3648/article/details/101129843

智能推荐

计算机网络中的默认网关是什么意思,路由器默认网关是什么意思?_少爷徐的博客-程序员秘密

默认网关的意思是一台主机如果找不到可用的网关,就把数据包发给默认指定的网关,由这个网关来处理数据包。现在主机使用的网关,一般指的是默认网关。 一台电脑的默认网关是不可以随随便便指定的,必须正确地指定,否则一台电脑就会将数据包发给不是网关的电脑,从而无法与其他网络的电脑通信。默认网关的设定有手动设置和自动设置两种方式下面为大家介绍路由器默认网关。路由器默认网关1、路由器IP地址的名称,与本地网络连接...

warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use __jxtgddlt的博客-程序员秘密

<br />使用VS2005以上版本(VS2005、VS2008、VS2010)编译在其他编译器下正常通过的C语言程序,你可能会遇到类似如下的警告提示:<br /> 引用内容<br />warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online

Servlet--HttpServletResponse的2个操作流的方法_LinkinPark8林肯的博客-程序员秘密

前面已经说过无数多次了,我们的项目都是基于HTTP协议的一次请求,一次响应。实际编码中,我们在处理完逻辑后一般是跳转到一个页面上,或者用输出流返回json字符串。其实跳转到一个页面往往也就是JSP,JSP运行在tomcat里面编译处理后返回响应,最后一步都是通过response获得输出流来返回到浏览器。这里总结下response的2个输出流。首先我们翻下API:public Ser

html中字符串写入文件路径,html5加js实现本地文件读取和写入并获取本地文件路径..._工程师李涛的博客-程序员秘密

最近在做课程设计,需要通过js读取本地文件,并且获取本地文件路径,然后在搜寻了一些资料,在此分享记录一下.HTML5提供了一台API可以实现文件的读写,文件读取利用API是FileReader代码如下:读取本地文件Document//点击导入按钮,使files触发点击事件,然后完成读取文件的操作$("#fileImport").click(function () {$("#files").clic...

Linux一句话精彩问答_linux报错unable to lacate package libness-myhostname_tianhuo_soft的博客-程序员秘密

  0001 修改主机名(陈绪) vi /etc/sysconfig/network,修改HOSTNAME一行为"HOSTNAME=主机名"(没有这行?那就添加这一行吧),然后运行命令 " hostname 主机名"。一般还要修改/etc/hosts文件中的主机名。这样,无论你是否重启,主机名都修改成功。 0002 Red Hat Linux启动到文字界面(不启动xwindow)(陈绪) vi

随便推点

浏览器和与服务器的交互过程&HTTPS工作流程_https和浏览器之间如何交互的_Code Life的博客-程序员秘密

web浏览器和web服务器的交互过程用户打开Chrom在地址栏输入 http://news.baidu.com/internet 会发生什么HTTP请求过程1.浏览器向DNS获取web服务器(www.baidu.com) 的IP地址;2.浏览器与IP地址为 115.239.211.112 的服务器进行TCP链接 端口为80;3.浏览器执行HTTP协议,发送GET /internet ...

设计APP测试用例需要考虑哪些维度?_app用例编写维度_我可太困了的博客-程序员秘密

一、APP的安装与升级1)升级中用户数据、设置、状态是否正常保留2)是否支持低版本、高版本的覆盖安装。覆盖安装后用户数据正常保存3)测试升级安装,升级安装后用户数据正常4)需考虑灰度升级的问题,提示是否友好,可以X掉5)强升是否正常,不升级app无法使用二、APP的启动与停止1)首次启动app正常进入loading页,loading页展示的时间、页面都正常2)启动时...

CentOS7_x86_64安装Oracle10g R2血泪史_sheen1991的博客-程序员秘密

CentOS7_86_64安装Oracle10g R2血泪史说明 本人是Linux菜鸟,在安装过程中,其实有些错误可以避免,但因为不了解走了一些弯路,不过自己学到了一些东西。这里整理了下遇到的各种问题和解决方式,是自己的一个总结,也希望对需要的人有帮助吧。另外说明下主要流程参考了一位博友的文章,我这里只是结合自己遇到的问题梳理下,博客原文CentOS 6.3(x86_32)下安装Or

用java代码实现构造目录树_weixin_30799995的博客-程序员秘密

怎么用java代码实现上面这样的目录树?首先创建数据表每条数据记录自己的id以及父节点的id然后进入java代码部分:public String directory(String author) { StringBuffer treeHtml = new StringBuffer(); // 得到所有的目录词(包含全部字段)...

首次使用Ubuntu登录设置root密码<转载>_ubdc怎么登陆_Senvenno27的博客-程序员秘密

正确方法:输入命令:sudo passwd//在安装Ubuntu期间,填写了一个用户及密码。第一次登录只能只用这个用户。//!!! root用户不能进入图形界面,如果手残删除了全部普通用户,请看本人上一篇文章第一次输入你现在用户的密码后两次输入需要修改的root用户新密码本文转自http://www.linuxdiyf.com/viewarticle.php?id=

PORTESCAP小型有刷直流电机25GST2R82_胤旭机电的博客-程序员秘密

技术参数:-尺寸8到35 MM-速度5,500到11,000 RPM-连续电机扭矩0.66到158.6 MNM-空心杯转子设计-低转子惯量-REE线圈-高电机效率-高功率重量比-部分有刷直流电机采用钕磁铁-滑动轴承和滚珠轴承型型号:08GS6110NS61 ATHLONIX12G88 ATHLONIX12GS88 ATHLONI...

推荐文章

热门文章

相关标签