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, bhinkle@mathworks.com
% 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, bhinkle@mathworks.com
% 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

MATLAB 条形图或饼状图 图案填充的更多相关文章

  1. 47.QT-QChart之曲线图,饼状图,条形图使用

     1.使用准备 在pro中, 添加QT+= charts 然后在界面头文件中添加头文件并声明命名空间,添加: #include <QtCharts> QT_CHARTS_USE_NAMES ...

  2. php 生成饼状图,折线图,条形图 通用类 2

    生成饼状图,折线图,条形图通用的php类,这里使用的是国外的 HighCharts,前台页面别忘了调用HighCahrt  js HighCharts中文网站  http://www.hcharts. ...

  3. php 生成饼状图,折线图,条形图 通用类

    生成饼状图,折线图,条形图通用的php类,这里使用的是百度 Echart. Echart 官方网站  http://echarts.baidu.com/ <?php class Echarts ...

  4. Echarts生成饼状图、条形图以及线形图 JS封装

    1.在我们开发程序中,经常会用到生成一些报表,比方说饼状图,条形图,折线图等.不多说了,直接上封装好的代码,如下Echarts.js所示 以下代码是封装在Echarts.js文件中 /** * Cre ...

  5. jqPlot图表插件学习之饼状图和环状图

    一.准备工作 官网下载(笔者选择的是jquery.jqplot.1.0.8r1250.zip这个版本) 然后读者需要根据自己的情况新建一个项目并且按照如下的方式加载对应的js和css(因为笔者在VS2 ...

  6. BIP_开发案例08_BI Publisher图表示例 饼状图/直方图/折线图(案例)

    2014-12-25 Created By BaoXinjian

  7. PHP实现动态生成饼状图 (转载)

    <?php //变量定义,画椭圆弧时的角度大小 define("ANGLELENGTH", 10); /** * 绘制图片 * @param $title 3D图的标题 * ...

  8. PHP实现动态生成饼状图、柱状图和折线图(转载)

    PHP在图像操作方面的表现非常出色,我们只需借助可以免费得到的GD库便可以轻松实现图.表勾画.下面将分别介绍PHP实现的饼状图.折线图和柱状图以 及他们的使用方法,这几段代码的特点就是不需要再把它们复 ...

  9. JavaScript+svg绘制的一个饼状图

    结果: svg参考:https://www.w3.org/TR/SVG/<body onload='document.body.appendChild( pieChart([12,23,34,4 ...

随机推荐

  1. Android Eclipse keystore.jks文件生成,根据keystore密钥获取SHA1安全码 ,apk打包

    keystore.jks文件生成,打包APK 选中项目右键-> Android Tools->Export Signed Application Package ,如图: 之后 点击Nex ...

  2. UVA-4288 Cat vs. Dog (最大独立集)

    题目大意:猫狗大赛,s个猫和t个狗,有v个投票者,他们不是爱猫者就是爱狗者,所以爱猫者一定会对某条猫投晋级票,对某条狗投淘汰票,爱狗者亦然.现在已知这v个人的投票情况,应该使哪些畜牲晋级或淘汰才能使投 ...

  3. jQuery.prop() 函数详解

    prop()函数用于设置或返回当前jQuery对象所匹配的元素的属性值. 该函数属于jQuery对象(实例).如果需要删除DOM元素的属性,请使用removeProp()函数. 语法jQuery 1. ...

  4. BLE Android开发中的问题

    在此直说两个问题,第一是Android6.0 SDK23版本情况下开发的Android BLE APP,千万要记得在代码中申请到地理位置读取权限,否则你的APP在运行的时候会出现各种问题,另外就是除了 ...

  5. Java中的HashMap的工作原理是什么?

    问答题23 /120 Java中的HashMap的工作原理是什么? 参考答案 Java中的HashMap是以键值对(key-value)的形式存储元素的.HashMap需要一个hash函数,它使用ha ...

  6. Android 平台代号、版本、API 级别和 NDK 版本

    代号.标记和细分版本号 简要来说,Android 的开发是围绕着版本系列进行的,这些版本使用美味的点心名字(按字母顺序)作为代号. 平台代号.版本.API 级别和 NDK 版本 为方便起见,代号与以下 ...

  7. Roofline Model与深度学习模型的性能分析

    原文链接: https://zhuanlan.zhihu.com/p/34204282 最近在不同的计算平台上验证几种经典深度学习模型的训练和预测性能时,经常遇到模型的实际测试性能表现和自己计算出的复 ...

  8. Scss开发临时学习过程||webpack、npm、gulp配置

    SCSS语法: 假设变量申明带有!default,那么如果在此申明之前没有这个变量的申明,则用这个值,反之如果之前有申明,则用申明的值. ‘...’传递多个参数: @mixin box-shadow( ...

  9. hibernate映射xml文件配置之一对多,多对多

    一对多配置 [1]班级和学生模型 --->班级可容纳多个学生 --->学生只能属于一个班级 [2]一对多配置中的关系维护(inverse) --->一端放弃关系的维护 ---> ...

  10. java实现MsOffice文档向pdf转化之OpenOffice软件

    本篇文档实现功能,将word和excel,ppt文档的文件转化成pdf格式的文档 第一步:下载第三方软件OpenOffice软件(不同的操作系统下载不同的版本) 下载地址:http://www.ope ...