Matlab是强大的工具,是众所周知的画图仿真工具;Matlab不仅可以画柱状图,饼状图,曲线图,直方图等二维图型;而且可以画三维或多维图。这里主要介绍在柱状图和饼状图内填充条纹,由于Matlab画图工具箱内无填充工具,这里主要通过命令来实现。
工具/原料
Matlab2016a
方法/步骤
1、导入几组数据,通过命令得到柱状图或者饼状图。示例:以导入txt文件格式为例;
2、弹出柱状图界面和饼状图界面。可见,柱状图和饼状图为彩色图片,有时我们不要彩色图片,要黑白图像。对黑白图像如何区分不同类别的数据,就需要往图像里面填充不同的条纹。
3、打开编辑—图形属性,更改自己所需要的字体,坐标轴范围,图例,线宽等;也可用命令修改,如:
4、下面就是重点:如何把上述兔脒巛钒图像转换成黑白图像,并填充不同条纹。首先,创建名为“applyhatch.m”的函数脚本。在同一路径下。代码如下:——复制即可。functionapplyhatch烫喇霰嘴(h,patterns,colorlist)%APPLYHATCHApplyhatchedpatternstoafigure%APPLYHATCH(H,PATTERNS)createsanewfigurefromthefigureHby%replacingdistinctcolorsinHwiththeblackandwhite%patternsinPATTERNS.TheformatforPATTERNScanbe%astringofthecharacters'/','\','|','-','+','x','.'%acellarrayofmatricesofzeros(white)andones(black)%%APPLYHATCH(H,PATTERNS,COLORS)mapsthecolorsinthenby3%matrixCOLORStoPATTERNS.EachrowofCOLORSspecifiesanRGB%colorvalue.%%NotethisfunctionmakesabitmapimageofHandsoislimited%tolow-resolution,bitmapoutput.%%Example1:%bar(rand(3,4));%applyhatch(gcf,'\-x.');%%Example2:%colormap(cool(6));%pie(rand(6,1));%legend('Jan','Feb','Mar','Apr','May','Jun');%applyhatch(gcf,'|-+.\/',cool(6));%%Seealso:MAKEHATCH%ByBenHinkle,bhinkle@mathworks.com%Thiscodeisinthepublicdomain.oldppmode=get(h,'paperpositionmode');oldunits=get(h,'units');set(h,'paperpositionmode','auto');set(h,'units','pixels');figsize=get(h,'position');ifnargin==2colorlist=[];endbits=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);[colors,colori]=nextnonbw(0,colorlist,bits);elsecolors=(bits(:,:,1)~=bits(:,:,2))|...(bits(:,:,1)~=bits(:,:,3));endpati=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);ifiscell(patterns)pattern=patterns{pati};elseifisa(patterns,'char')pattern=makehatch(patterns(pati));elsepattern=patterns;endpattern=uint8(255*(1-pattern));pheight=size(pattern,2);pwidth=size(pattern,1);ratioh=ceil(bheight/pheight);ratiow=ceil(bwidth/pwidth);bigpattern=repmat(pattern,[ratiohratiow]);ifratioh*pheight>bheightbigpattern(bheight+1:end,:)=[];endifratiow*pwidth>bwidthbigpattern(:,bwidth+1:end)=[];endbigpattern=repmat(bigpattern,[113]);color=(bits(:,:,1)==colorval(1))&...(bits(:,:,2)==colorval(2))&...(bits(:,:,3)==colorval(3));color=repmat(color,[113]);bits(color)=bigpattern(color);if~isempty(colorlist)[colors,colori]=nextnonbw(colori,colorlist,bits);elsecolors=(bits(:,:,1)~=bits(:,:,2))|...(bits(:,:,1)~=bits(:,:,3));endcolorind=find(colors);pati=(pati+1);ifpati>length(patterns)pati=1;endendnewfig=figure('units','pixels','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(imaxes,'position',[00figsize(3)figsize(4)+1],'visible','off');set(newfig,'visible','on');function[colors,out]=nextnonbw(ind,colorlist,bits)out=ind+1;colors=[];whileout<=size(colorlist,1)ifisequal(colorlist(out,:),[255255255])|...isequal(colorlist(out,:),[000])out=out+1;elsecolors=(colorlist(out,1)==bits(:,:,1))&...(colorlist(out,2)==bits(:,:,2))&...(colorlist(out,3)==bits(:,:,3));returnendend%而applyhatch函数需要调用下面的函数functionA=makehatch(hatch)%MAKEHATCHPredefinedhatchpatterns%MAKEHATCH(HATCH)returnsamatrixwiththehatchpatternforHATCH%accordingtothefollowingtable:%HATCHpattern%----------------%/right-slantedlines%\left-slantedlines%|verticallines%-horizontallines%+crossingverticalandhorizontallines%xcriss-crossinglines%.singledots%%Seealso:APPLYHATCH%ByBenHinkle,bhinkle@mathworks.com%Thiscodeisinthepublicdomain.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;otherwiseerror(['Undefinedhatchpattern"'hatch'".']);end
5、柱状填充图和饼状填充图,画图命令如下佼沣族昀:a=importdata('xxx.txt');>>b=importdata(泌驾台佐39;xxx.txt');>>c=importdata('xxx.txt');>>d=[a,b,c];>>bar(d);警告:MATLAB已通过改用OpenGL软件禁用了某些高级的图形渲染功能。欲了解有关详细信息,请点击此处。>>figure(2)>>pie(d);>>axis([0110.022]);>>legend('方法1','方法2','方法3');>>set(gcf,'color','white');>>applyhatch(gcf,'x+\.');
6、结果:不同条纹填充结果柱状图和饼状图如图所示: