如何在Matlab中画有阴影的柱状图.docx

上传人:b****7 文档编号:23587084 上传时间:2023-05-18 格式:DOCX 页数:24 大小:47.99KB
下载 相关 举报
如何在Matlab中画有阴影的柱状图.docx_第1页
第1页 / 共24页
如何在Matlab中画有阴影的柱状图.docx_第2页
第2页 / 共24页
如何在Matlab中画有阴影的柱状图.docx_第3页
第3页 / 共24页
如何在Matlab中画有阴影的柱状图.docx_第4页
第4页 / 共24页
如何在Matlab中画有阴影的柱状图.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

如何在Matlab中画有阴影的柱状图.docx

《如何在Matlab中画有阴影的柱状图.docx》由会员分享,可在线阅读,更多相关《如何在Matlab中画有阴影的柱状图.docx(24页珍藏版)》请在冰豆网上搜索。

如何在Matlab中画有阴影的柱状图.docx

如何在Matlab中画有阴影的柱状图

在画柱状图时,利用不同的颜色区分在实际打印时经常区分不出。

为了能够更清楚第表示最终结果,可采用在柱状图中打阴影的方式来表示。

然而,Matalab本身没有提供实现这种功能的函数库。

下面两个函数:

%ApplyBrandon'sfunction

applyhatch_pluscolor

%ApplyBrian'sfunction

applyhatch_plusC(fH,'\-x.','rkbk');

实现了上述功能。

这两个函数中都调用了函数makehatch_plus源码如下(都已经调试通过,可方便使用)。

fH=gcf;colormap(jet(4));

h=bar(rand(3,4));

legend('Apple','Orange','Banana','Melon','Location','EastOutside');

%ApplyBrandon'sfunction

tH=title('Brandon''sapplyhatch');

applyhatch_pluscolor(fH,'\-x.',0,[1010],jet(4));

%ApplyBrian'sfunction

set(tH,'String','Brian''sapplyhatch');

applyhatch_plusC(fH,'\-x.','rkbk');

set(tH,'String','Original');

代码源自:

function[im_hatch,colorlist]=applyhatch_pluscolor(h,patterns,CvBW,Hinvert,colorlist,...

dpi,hatchsc,lw)

%APPLYHATCH_PLUSCOLORApplyhatchedpatternstoafigureinBWorColor

%APPLYHATCH_PLUSCOLOR(H,PATTERNS)createsanewfigurefromthefigureHby

%replacingdistinctcolorsinHwiththeblackandwhite

%patternsinPATTERNS.TheformatforPATTERNScanbe

%astringofthecharacters:

%'/','\','|','-','+','x','.','c','w','k'

%(seemakehatch_plus.mformoredetails)or

%acellarrayofmatricesofzeros(white)andones(black)

%

%Inaddition,Hcanalternativelybeauint8NxMx3matrixofthetype

%producedbyimread.Inthiscase,colorsinthisimagewillbe

%replacedwithpatternsasifitwasafigure.Afinalfigurewindow

%willbegeneratedthatdisplaystheresult.TheDPIargument

%discussedbelowwillbeignoredifHisanimagematrix.

%

%APPLYHATCH_PLUSCOLOR(H,PATTERNS,CVBW)binaryvalueforchoiceofColororBlack

%andWhiteplots.Ifcolorischosenthecolorwillmatchthatofthe

%currentfill.1->Color,anythingelse->BW

%

%APPLYHATCH_PLUSCOLOR(H,PATTERNS,CVBW,HINVERT)binaryvaluetoinvertthehatch.

%i.e.,ifitisblacklineswithawhitebackground,thatbecomeswhite

%lineswithablackbackground.Thiscaneitherbeascalarvalueora

%1xNarrayequaltothelengthofPATTERNS.Whenusedasanarrayeach

%PATTERNS(i)willbeinvertedaccordingtoHinvert(i).1->Invert,

%anythingelse->NonInverted

%

%APPLYHATCH_PLUSCOLOR(H,PATTERNS,CVBW,HINVERT,COLORS)mapsthecolorsinthenby3

%matrixCOLORStoPATTERNS.EachrowofCOLORSspecifiesanRGB

%colorvalue.

%

%NotethisfunctionmakesabitmapimageofHandsoislimited

%tobitmapoutput.

%

%Additionalarguments:

%

%[im_hatch,colorlist]=applyhatch_plus(h,patterns,CvBW,Hinvert,colorlist,dpi,hatchsc,linewidth)

%

%inputDPIallowsspecificationofbitmapresolution,makingplotresolution

%betterforprinting.IgnoredifHisanimagematrix.

%HATCHSCmultiplierforhatchscaletoincreasesizeofpatternforbetteroperation

%athigherresolutions

%default[]usesscreenresolutionasin

%APPLYHATCH

%LINEWIDTHAscalingfactortoapplytolineanddotsizes

%inhatching.Defaultsto1.

%outputIM_HATCHRGBbitmapmatrixofnewfigure

%useIMWRITEtooutputindesiredformat

%COLORLISTListofcolorsactuallyreplaced.Usefulinfoif

%nocolorlistinitiallygiventofunction.

%Colorlistwillbeuint8,not0-1scaleas

%originallyspecified.

%

%Example1:

%bar(rand(3,4));

%[im_hatch,colorlist]=applyhatch_pluscolor(gcf,'\-x.',0,0,[],150);

%imwrite(im_hatch,'im_hatch.png','png')

%

%Example2:

%bar(rand(3,4));

%[im_hatch,colorlist]=applyhatch_pluscolor(gcf,'\-x.',1,[],[],150);

%imwrite(im_hatch,'im_hatch.png','png')

%

%Example3:

%colormap(cool(6));

%pie(rand(6,1));

%legend('Jan','Feb','Mar','Apr','May','Jun');

%im_hatch=applyhatch_pluscolor(gcf,'|-.+\/',1,[110100],cool(6),200,3,2);

%imwrite(im_hatch,'im_hatch.png','png')

%

%Example4:

Producesroughlythesamethingasexample1

%bar(rand(3,4));

%print-dtiff-r150im.tiff

%im=imread('im.tiff','tiff');

%[im_hatch,colorlist]=applyhatch_pluscolor(im,'\-x.');

%imwrite(im_hatch,'im_hatch.tiff','tiff')

%

%

%ModificationofAPPLYHATCHtoallowhigherresolutionoutput

%ModifiedBrianFGKatz8-aout-03

%ModifiedDavidMKaplan19-fevrier-08

%

%ModificationofAPPLYHATCH_PLUStoallowforcolorandinvertedhatch

%ModifiedBrandonLeveyMay6,2009

%

%Seealso:

APPLYHATCH,APPLYHATCH_PLUS,MAKEHATCH,MAKEHATCH_PLUS

%ByBenHinkle,*********************

%Thiscodeisinthepublicdomain.

 

if~exist('CvBW','var');CvBW=0;end%defaultstoblackandwhite

ifisempty(CvBW);CvBW=0;end%defaultstoblackandwhite

if(CvBW~=0&&CvBW~=1);CvBW=0;end%defaultstoblackandwhite

if~exist('Hinvert','var');Hinvert=0;end%defaultstonotinverted

ifisempty(Hinvert);Hinvert=0;end%defaultstonotinverted

iflength(Hinvert)==length(patterns)||length(Hinvert)==1

fori=1:

length(Hinvert)

ifHinvert(i)~=0&&Hinvert(i)~=1;Hinvert(i)=0;end

end

else

error(['ThelengthofHinvertmustbe1orequaltothelengthofPATTERNS']);

end

if~exist('hatchsc','var');hatchsc=1;end

if~exist('dpi','var');dpi=0;end%defaultstoscreenresolution

if~exist('colorlist','var');colorlist=[];end

if~exist('lw','var');lw=1;end

ifnumel(h)==1%Assumeitisafigurewindow

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',[111]);

figsize=get(h,'position');

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

%%Tryadifferentapproachusingatemporaryfile-usethisifhavingprobs

%tn=[tempname'.tif'];

%print('-dtiff',['-r'num2str(dpi)],tn)

%bits=uint8(imread(tn,'TIFF'));

%delete(tn)

set(h,'paperpositionmode',oldppmode);

set(h,'color',oldcolor);

elseifsize(h,3)==3%Assumeitisanimagematrix

bits=h;

oldunits='pixels';

oldpos=[0,0,size(bits,2),size(bits,1)];

figsize=oldpos;

else

error('Badfirstargument.');

end

bwidth=size(bits,2);

bheight=size(bits,1);

bsize=bwidth*bheight;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Thenextbitbasicallymodernizestheoriginal

%versionofthisfunctionusingthingslikeunique

%andforloops

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Makebitmaponelongmatrixwith3columns

bits=reshape(bits,[bsize,3]);

%Convertoriginalcolorscaleto255scale

if~isempty(colorlist)

%NOTE:

Added"floor"belowbecausethisseemstobetterpickout

%correctcolorsproducedby"hardcopyabovebetterthanuint8byitself

%colorlist=uint8(255*colorlist);

colorlist=uint8(floor(255*colorlist));

else

%Finduniquecolorsinimage-thistakesalongtimeathighresolution

[B,I,J]=unique(bits,'rows');

switchCvBW

case0%BWplot

%Findjust"colored"colors

C=find(B(:

1)~=B(:

2)|B(:

1)~=B(:

3));

case1%colorplot

%Findallnonblackandwhite

B=sortrows(B);

C=1:

size(B,1);

C=C(2:

end-1)';

end

colorlist=B(C,:

);

end

%Loopoverlistofcolorsandfindmatches

fork=1:

size(colorlist,1)

%Findpointsthatmatchcolor

ifexist('B','var')%Useuniquecolorsifaround

I=C(k)==J;

else%Otherwisetesteachpoint

cc=colorlist(k,:

);

I=bits(:

1)==cc

(1)&bits(:

2)==cc

(2)&bits(:

3)==cc(3);

if~any(I(:

)),continue,end

end

%Whatpatterntouse

pati=mod(k-1,numel(patterns))+1;

ifiscell(patterns)

pattern=patterns{pati};

elseifisa(patterns,'char')

pattern=makehatch_plus(patterns(pati),6*hatchsc,lw);

else

pattern=patterns;

end

pattern=uint8(1-pattern);

iflength(Hinvert)==1

invertHatch=logical(Hinvert);

else

invertHatch=logical(Hinvert(pati));

end

%Makeabigpatternmatchingsizeofbits

pheight=size(pattern,2);

pwidth=size(pattern,1);

ratioh=ceil(bheight/pheight);

ratiow=ceil(bwidth/pwidth);

bigpattern=repmat(pattern,[ratiohratiow]);

ifratioh*pheight>bheight

bigpattern(bheight+1:

end,:

)=[];

end

ifratiow*pwidth>bwidth

bigpattern(:

bwidth+1:

end)=[];

end

%PutthatpatternintobitsandlogicalvaluesbasedonCvBWandHinvert

switchCvBW

case0%BW

ifinvertHatch

bits(find(I),:

)=repmat(~bigpattern(I)*255,[1,3]);

else

bits(find(I),:

)=repmat(bigpattern(I)*255,[1,3]);

end

case1%Color

ifinvertHatch

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

%Putbitsbackintoitsnormalshape

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',[00figsize(3)figsize(4)+1],'visible','off');

set(newfig,'visible','on');

set(newfig,'units','normalized');

set(imaxes,'units','normalized');

set(imaxes,'DataAspectRatio',[111],'DataAspectRatioMode','manual');

 

ifnargout>0,im_hatch=bits;end

ifnargout<2,cl

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 其它语言学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1