• MATLAB | 面积图、饼状图、水平柱状图的斜线填充(阴影填充)


    在这里插入图片描述
    在这里插入图片描述

    没想到叭,阴影柱状图仅仅一天就迎来了(1.5.1)版本,已经支持水平柱状图绘制!!同时本人又连夜赶制了另外两款相关的阴影图绘制函数:


    来来来,后文马上介绍一下这三款函数。

    另:代码开发基于polyshape对象,阴影柱状图绘制函数理论上需要至少R2017b及之后版本才能使用(越新越好)。(polyshape yyds)


    区域填充函数

    先讲解此函数如何使用,再在此部分最后给出完整代码:

    基本使用

    设置为角度为pi/3,80根线条进行填充:

    t=0:.01:2*pi;
    X=cos(t);
    Y=sin(t);
    
    plot(X,Y,'LineWidth',2);
    hold on;axis equal
    shadowFill(X,Y,pi/3,80);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    绘制阴影面积图

    t=0:.01:2*pi;
    y=sin(t); 
    
    plot(t,y,'LineWidth',2);
    hold on;axis equal
    shadowFill(t,y,pi/4,80);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    绘制两函数中间区域

    这个就比较复杂了,给个例子:

    t=-.1:.01:pi/2;
    y1=sin(t).*2;
    y2=t.^2;
    
    hold on
    plot(t,y1,'LineWidth',2);
    plot(t,y2,'LineWidth',2);
    
    diffy=y1-y2;
    tpos=find(diffy>=0);
    T=[t(tpos(1):tpos(end)),t(tpos(end):-1:tpos(1))];
    Y=[y1(tpos(1):tpos(end)),y2(tpos(end):-1:tpos(1))];
    
    
    shadowFill(T,Y,pi/5,70);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    设置填充颜色和格式

    t=-.1:.01:pi/2;
    y1=sin(t).*2;
    y2=t.^2;
    
    hold on
    plot(t,y1,'LineWidth',2);
    plot(t,y2,'LineWidth',2);
    
    diffy=y1-y2;
    tpos=find(diffy>=0);
    T=[t(tpos(1):tpos(end)),t(tpos(end):-1:tpos(1))];
    Y=[y1(tpos(1):tpos(end)),y2(tpos(end):-1:tpos(1))];
    
    
    shadowFill(T,Y,pi/5,70,'LineStyle',':','Color',[.2,.2,.9]); 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    工具函数完整代码

    function SF=shadowFill(X,Y,theta,num,varargin)
    % @author : slandarer
    % gzh  : slandarer随笔
    if theta>pi/2||theta<-pi/2
        error('Inner theta should be in range of [-pi/2,pi/2]');
    end
    % 基础属性获取
    ax=gca;hold on;
    try
    pshape=polyshape(X,Y);
    catch
    end
    XYmin=[min(X),min(Y)];
    XYmax=[max(X),max(Y)];
    diffY=max(Y)-min(Y);
    
    % 获取阴影线数值
    if abs(theta)<eps
        YY=linspace(XYmin(2),XYmax(2),num);
        tXX=zeros(3,num);tYY=zeros(3,num);
        for i=1:num
            [in,~]=intersect(pshape,[XYmin(1),YY(i);XYmax(2),YY(i)]);
            if ~isempty(in)
                tXX(:,i)=[in(1,1);in(end,1);nan];
                tYY(:,i)=[in(1,2);in(end,2);nan];
            else
                tXX(:,i)=[nan;nan;nan];
                tYY(:,i)=[nan;nan;nan];
            end
        end
    else
        cott=cot(theta);
        XX1=linspace(XYmin(1)-cott.*diffY,XYmax(1),num);
        XX2=linspace(XYmin(1),XYmax(1)+cott.*diffY,num);
        tXX=zeros(3,num);tYY=zeros(3,num);
        for i=1:num
            [in,~]=intersect(pshape,[XX1(i),XYmin(2);XX2(i),XYmax(2)]);
            if ~isempty(in)
                tXX(:,i)=[in(1,1);in(end,1);nan];
                tYY(:,i)=[in(1,2);in(end,2);nan];
            else
                tXX(:,i)=[nan;nan;nan];
                tYY(:,i)=[nan;nan;nan];
            end
        end
    end
    % 绘制阴影
    SF=plot(ax,tXX(:),tYY(:),'LineWidth',.5,'Color',[0,0,0],varargin{:},'Tag','shadowFill');
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    阴影饼状图

    阴影有:
    \,/,_,|,+,x,.,w,k,g
    十种格式,可以自己都试一下:

    基本使用

    X=[1 3 0.5 2.5 2];
    
    SP=shadowPie(X,'ShadowType',{'/','.','|','+','x'});
    SP=SP.draw(); 
    
    • 1
    • 2
    • 3
    • 4

    带偏移饼图

    X=[1 3 0.5 2.5 2];
    explode=[0 1 0 5 0];
    
    SP=shadowPie(X,explode,'ShadowType',{'/','.','|','+','x'});
    SP=SP.draw(); 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    添加图例

    X=[1 3 0.5 2.5 2];
    explode=[0 1 0 5 0];
    
    SP=shadowPie(X,explode,'ShadowType',{'/','.','|','+','x'});
    SP=SP.draw();
    
    SP.legend({'AAA','BBB','CCC','DDD','EEE'},'FontSize',13,'FontName','Cambria'); 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    图例可能会被标签信息挡住,可以自行拖拽图例到其他位置:

    基本修饰

    阴影、柱状图框、图例框分别具有pieShadow,pieBox,lgdBox的标签,使用findobj获取对象并循环修饰即可,不过为了绘制更好看,部分阴影使用plot绘制部分则是使用scatter绘制,因此阴影修饰时要用if先检测一下格式,给个示例:

    X=[1 3 0.5 2.5 2];
    explode=[0 1 0 5 0];
    
    SP=shadowPie(X,explode,'ShadowType',{'/','.','|','+','x'});
    SP=SP.draw();
    
    SP.legend({'AAA','BBB','CCC','DDD','EEE'},'FontSize',13,'FontName','Cambria');
    
    % 修饰文字
    textHdl=findobj('Tag','pieText');
    for i=1:length(textHdl)
        textHdl(i).FontName='Cambria';
        textHdl(i).FontSize=14;
        textHdl(i).Color=[0,0,.8];
    end
    
    % 修饰阴影变成红色并加粗
    shadowHdl=findobj('Tag','pieShadow');
    for i=1:length(shadowHdl)
        if isa(shadowHdl(i),'matlab.graphics.chart.primitive.Line')
            shadowHdl(i).Color=[.8,.6,.6];
            shadowHdl(i).LineWidth=1;
        else
            shadowHdl(i).MarkerFaceColor=[.8,.6,.6];
            shadowHdl(i).SizeData=5;
        end
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    光看上面代码大佬们应该就已经懂了咋用,如果比较小白,可以直接使用MATLAB自带的属性修饰器:

    点击:查看->属性编辑器

    点击选中对象即可立即修饰颜色、粗细等各种格式:

    工具函数完整代码

    classdef shadowPie
    % @author : slandarer
    % gzh  : slandarer随笔
    
        properties
            ax,XData,num
    
            shadowTypeList={'\','/','_','|','+','x','.','w','k','g'}
            shadowType % 阴影类型
            otherProp  % 其他初始属性
    
            oriPieHdl  % 原始图形句柄
            newPieHdl
            pshapeHdl  % polyshape图形句柄
            lineNum=85 % 阴影基础线数量
    
            oriLegend;XYRate;nonePieHdl
    
            lL_X1;lL_X2;cL_X1;cL_X2;hL_X1;hL_X2;XMesh;YMesh
        end
    
        methods
            function obj=shadowPie(varargin)
                % 基础属性设置
                obj.XData=varargin{1};varargin(1)=[];
                if any(strcmpi('shadowType',varargin))
                    tind=find(strcmpi('shadowType',varargin));
                    obj.shadowType=varargin{tind+1};
                    varargin([tind,tind+1])=[];
                else
                    obj.shadowType=obj.shadowTypeList;
                end
                if any(strcmpi('lineNum',varargin))
                    tind=find(strcmpi('lineNum',varargin));
                    obj.lineNum=varargin{tind+1};
                    varargin([tind,tind+1])=[];
                end
                obj.otherProp=varargin;
                obj.num=length(obj.XData);
                help shadowPie
            end
    
            function obj=draw(obj)
                % 基础绘图
                obj.oriPieHdl=pie(obj.XData,obj.otherProp{:});
                obj.ax=gca;hold(obj.ax,'on');
    
    
                % 一些基础线
                obj.lL_X1=linspace(-1.5-6,1.5,obj.lineNum);
                obj.lL_X2=linspace(-1.5,1.5+6,obj.lineNum);
    
                obj.cL_X1=linspace(-1.5-3,1.5,obj.lineNum);
                obj.cL_X2=linspace(-1.5,1.5+3,obj.lineNum);
    
                obj.hL_X1=linspace(-1.5,1.5,obj.lineNum);
                obj.hL_X2=linspace(-1.5,1.5,obj.lineNum);
    
                [obj.XMesh,obj.YMesh]=meshgrid(linspace(-1.5,1.5,obj.lineNum));
    
                n=1;
                for i=1:length(obj.oriPieHdl)
                    if isa(obj.oriPieHdl(i),'matlab.graphics.primitive.Patch')
                        obj.newPieHdl(n)=obj.oriPieHdl(i);
                        obj.oriPieHdl(i).Tag='pieBox';
                        n=n+1;
                    else
                        obj.oriPieHdl(i).Tag='pieText';
                    end
                end
    
                n=1;
                for i=1:length(obj.oriPieHdl)
                    if isa(obj.oriPieHdl(i),'matlab.graphics.primitive.Patch')
                        obj.oriPieHdl(i).FaceColor=[1,1,1];
                        obj.oriPieHdl(i).LineWidth=.8;
                        tPolyPhape=polyshape(obj.oriPieHdl(i).XData,obj.oriPieHdl(i).YData);
                        tType=mod(n-1,length(obj.shadowType))+1;
                        switch obj.shadowType{tType}
                            case '\'
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.lL_X1(k),1.5;obj.lL_X2(k),-1.5]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end     
                                end
                                plot(tXX(:),tYY(:),'Color',[0,0,0],'LineWidth',.5,'Tag','pieShadow')
                            case '/'
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.lL_X1(k),-1.5;obj.lL_X2(k),1.5]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end     
                                end
                                plot(tXX(:),tYY(:),'Color',[0,0,0],'LineWidth',.5,'Tag','pieShadow')
                            case '_'
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[-1.5,obj.hL_X1(k);1.5,obj.hL_X2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end     
                                end
                                plot(tXX(:),tYY(:),'Color',[0,0,0],'LineWidth',.5,'Tag','pieShadow')
                            case '|'
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.hL_X1(k),-1.5;obj.hL_X2(k),1.5]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end     
                                end
                                plot(tXX(:),tYY(:),'Color',[0,0,0],'LineWidth',.5,'Tag','pieShadow')
                            case '+'
                                tXX1=zeros([3,obj.lineNum]);tYY1=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[-1.5,obj.hL_X1(k);1.5,obj.hL_X2(k)]);
                                    if ~isempty(in)
                                        tXX1(:,k)=[in(1,1);in(end,1);nan];
                                        tYY1(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX1(:,k)=[nan;nan;nan];
                                        tYY1(:,k)=[nan;nan;nan];
                                    end     
                                end
                                tXX2=zeros([3,obj.lineNum]);tYY2=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.hL_X1(k),-1.5;obj.hL_X2(k),1.5]);
                                    if ~isempty(in)
                                        tXX2(:,k)=[in(1,1);in(end,1);nan];
                                        tYY2(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX2(:,k)=[nan;nan;nan];
                                        tYY2(:,k)=[nan;nan;nan];
                                    end     
                                end
                                plot([tXX1(:);nan;tXX2(:)],[tYY1(:);nan;tYY2(:)],'Color',[0,0,0],'LineWidth',.5,'Tag','pieShadow')
                            case 'x'
                                tXX1=zeros([3,obj.lineNum]);tYY1=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.cL_X1(k),1.5;obj.cL_X2(k),-1.5]);
                                    if ~isempty(in)
                                        tXX1(:,k)=[in(1,1);in(end,1);nan];
                                        tYY1(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX1(:,k)=[nan;nan;nan];
                                        tYY1(:,k)=[nan;nan;nan];
                                    end     
                                end
                                tXX2=zeros([3,obj.lineNum]);tYY2=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.cL_X1(k),-1.5;obj.cL_X2(k),1.5]);
                                    if ~isempty(in)
                                        tXX2(:,k)=[in(1,1);in(end,1);nan];
                                        tYY2(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX2(:,k)=[nan;nan;nan];
                                        tYY2(:,k)=[nan;nan;nan];
                                    end     
                                end
                                plot([tXX1(:);nan;tXX2(:)],[tYY1(:);nan;tYY2(:)],'Color',[0,0,0],'LineWidth',.5,'Tag','pieShadow')
                            case '.'
                                 tXX=obj.XMesh(:);tYY=obj.YMesh(:);
                                 tbool=isinterior(tPolyPhape,tXX(:),tYY(:));
                                 scatter(tXX(tbool),tYY(tbool),2,'filled','o','MarkerEdgeColor','none','MarkerFaceColor',[0,0,0],'Tag','pieShadow')
                            case 'w'
                                obj.oriPieHdl(i).FaceColor=[1,1,1];
                            case 'k' 
                                obj.oriPieHdl(i).FaceColor=[0,0,0];
                            case 'g'
                                obj.oriPieHdl(i).FaceColor=[.5,.5,.5];
                        end
                        n=n+1;
                    end
                end
            end
            function obj=legend(obj,cellStr,varargin)
                obj.nonePieHdl=pie(obj.XData,obj.otherProp{:});
                nn=1;
                for n=1:length(obj.nonePieHdl)
                    if isa(obj.nonePieHdl(n),'matlab.graphics.primitive.Patch')
                        obj.nonePieHdl(n).FaceColor='none';
                        obj.nonePieHdl(n).EdgeColor='none';
                        newNonePieHdl(nn)=obj.nonePieHdl(n);
                        nn=nn+1;
                    else
                        obj.nonePieHdl(n).Visible='off';
                    end
                end
                obj.oriLegend=legend(newNonePieHdl,cellStr,varargin{:});
                obj.oriLegend.AutoUpdate='off';
                obj.oriLegend.Box='off';
                obj.ax.XLim=obj.ax.XLim;
                obj.ax.YLim=obj.ax.YLim;
    
                % 框重定位
                tlgdPos=obj.oriLegend.Position;
                taxPos=obj.ax.Position;
                taxXLim=obj.ax.XLim;
                taxYLim=obj.ax.YLim;
                tfigRate=obj.ax.Parent.Position(3:4);
                tfigRate=tfigRate.*taxPos(3:4);tfigRate=tfigRate./min(tfigRate);
                taxXLim=taxXLim.*tfigRate(1);
                taxYLim=taxYLim.*tfigRate(2);
    
                tXYMin=(tlgdPos(1:2)-taxPos(1:2))./taxPos(3:4).*[diff(taxXLim),diff(taxYLim)]+[taxXLim(1),taxYLim(1)];
                tXYMax=(tlgdPos(1:2)+tlgdPos(3:4)-taxPos(1:2))./taxPos(3:4).*[diff(taxXLim),diff(taxYLim)]+[taxXLim(1),taxYLim(1)];
                boxHdl=fill([tXYMin(1),tXYMax(1),tXYMax(1),tXYMin(1)],[tXYMin(2),tXYMin(2),tXYMax(2),tXYMax(2)],[1,1,1],'Tag','lgdBox');
    
                for n=1:length((obj.newPieHdl))
                    ttType=mod(n-1,length(obj.shadowType))+1;
                    squareHdl(n)=fill([0,0,0],[0,0,0],[1,1,1],'EdgeColor',[0,0,0],'LineWidth',.7,'Tag','pieBox');
                    if strcmp(obj.shadowType{ttType},'.')
                        lgdLineHdl(n)=scatter(0,0,2,'filled','o','MarkerEdgeColor','none','MarkerFaceColor',[0,0,0],'Tag','pieShadow');
                    else
                        lgdLineHdl(n)=plot(0,0,'Color',[0,0,0],'LineWidth',.5,'Tag','barBox','Tag','pieShadow');
                    end
                end
                moveLgd()
                set(obj.ax.Parent,'WindowButtonMotionFcn',@moveLgd);
                function moveLgd(~,~)
                    % 框重定位
                    lgdPos=obj.oriLegend.Position;
                    axPos=obj.ax.Position;
                    axXLim=obj.ax.XLim;
                    axYLim=obj.ax.YLim;
                    figRate=obj.ax.Parent.Position(3:4);
                    figRate=figRate.*axPos(3:4);figRate=figRate./min(figRate);
                    axXLim=axXLim.*figRate(1);
                    axYLim=axYLim.*figRate(2);
    
                    XYMin=(lgdPos(1:2)-axPos(1:2))./axPos(3:4).*[diff(axXLim),diff(axYLim)]+[axXLim(1),axYLim(1)];
                    XYMax=(lgdPos(1:2)+lgdPos(3:4)-axPos(1:2))./axPos(3:4).*[diff(axXLim),diff(axYLim)]+[axXLim(1),axYLim(1)];
                    boxHdl.XData=[XYMin(1),XYMax(1),XYMax(1),XYMin(1)];
                    boxHdl.YData=[XYMin(2),XYMin(2),XYMax(2),XYMax(2)];
    
                    Y=XYMin(2)+(XYMax(2)-XYMin(2)).*linspace(1/length(obj.newPieHdl)/2+1/50,1-1/length(obj.newPieHdl)/2-1/50,length(obj.newPieHdl));
                    obj.XYRate=diff(obj.ax.YLim)./diff(obj.ax.XLim);
    
                    PBA=obj.ax.PlotBoxAspectRatio(2)./obj.ax.PlotBoxAspectRatio(1)./0.7896;
                    Y=fliplr(Y);
                    for i=1:length((obj.newPieHdl))
                        tX=XYMin(1)+(XYMax(2)-XYMin(2))./2./length(obj.newPieHdl).*[1/10,.95,.95,1/10].*(PBA./obj.XYRate.*3);
                        tY=Y(i)+0.75.*[-1,-1,1,1].*(XYMax(2)-XYMin(2))./2./length(obj.newPieHdl);
                        squareHdl(i).XData=tX;
                        squareHdl(i).YData=tY;
    
                        tPolyPhape=polyshape(tX,tY);
                        tType=mod(i-1,length(obj.shadowType))+1;
    
                        switch obj.shadowType{tType}
                            case '\'
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.lL_X1(k),1.5;obj.lL_X2(k),-1.5]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end
                                lgdLineHdl(i).XData=tXX(:);
                                lgdLineHdl(i).YData=tYY(:);
                            case '/'
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.lL_X1(k),-1.5;obj.lL_X2(k),1.5]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end
                                lgdLineHdl(i).XData=tXX(:);
                                lgdLineHdl(i).YData=tYY(:);
                            case '_'
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[-1.5,obj.hL_X1(k);1.5,obj.hL_X2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end
                                lgdLineHdl(i).XData=tXX(:);
                                lgdLineHdl(i).YData=tYY(:);
                            case '|'
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.hL_X1(k),-1.5;obj.hL_X2(k),1.5]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end
                                lgdLineHdl(i).XData=tXX(:);
                                lgdLineHdl(i).YData=tYY(:);
                            case '+'
                                tXX1=zeros([3,obj.lineNum]);tYY1=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[-1.5,obj.hL_X1(k);1.5,obj.hL_X2(k)]);
                                    if ~isempty(in)
                                        tXX1(:,k)=[in(1,1);in(end,1);nan];
                                        tYY1(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX1(:,k)=[nan;nan;nan];
                                        tYY1(:,k)=[nan;nan;nan];
                                    end
                                end
                                tXX2=zeros([3,obj.lineNum]);tYY2=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.hL_X1(k),-1.5;obj.hL_X2(k),1.5]);
                                    if ~isempty(in)
                                        tXX2(:,k)=[in(1,1);in(end,1);nan];
                                        tYY2(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX2(:,k)=[nan;nan;nan];
                                        tYY2(:,k)=[nan;nan;nan];
                                    end
                                end
                                lgdLineHdl(i).XData=[tXX1(:);nan;tXX2(:)];
                                lgdLineHdl(i).YData=[tYY1(:);nan;tYY2(:)];
                            case 'x'
                                tXX1=zeros([3,obj.lineNum]);tYY1=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.cL_X1(k),1.5;obj.cL_X2(k),-1.5]);
                                    if ~isempty(in)
                                        tXX1(:,k)=[in(1,1);in(end,1);nan];
                                        tYY1(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX1(:,k)=[nan;nan;nan];
                                        tYY1(:,k)=[nan;nan;nan];
                                    end
                                end
                                tXX2=zeros([3,obj.lineNum]);tYY2=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[obj.cL_X1(k),-1.5;obj.cL_X2(k),1.5]);
                                    if ~isempty(in)
                                        tXX2(:,k)=[in(1,1);in(end,1);nan];
                                        tYY2(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX2(:,k)=[nan;nan;nan];
                                        tYY2(:,k)=[nan;nan;nan];
                                    end
                                end
                                lgdLineHdl(i).XData=[tXX1(:);nan;tXX2(:)];
                                lgdLineHdl(i).YData=[tYY1(:);nan;tYY2(:)];
                            case '.'
                                tXX=obj.XMesh(:);tYY=obj.YMesh(:);
                                tbool=isinterior(tPolyPhape,tXX(:),tYY(:));
                                lgdLineHdl(i).XData=tXX(tbool);
                                lgdLineHdl(i).YData=tYY(tbool);
                            case 'w'
                                squareHdl(i).FaceColor=[1,1,1];
                            case 'k'
                                squareHdl(i).FaceColor=[0,0,0];
                            case 'g'
                                squareHdl(i).FaceColor=[.5,.5,.5];
                        end
                    end
                end
    
            end
        end
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393

    未经允许本代码请勿作商业用途,引用的话可以引用我file exchange上的链接,可使用如下格式:

    Zhaoxu Liu (2022). pie with shadow 阴影饼图 (https://www.mathworks.com/matlabcentral/fileexchange/117045-pie-with-shadow), MATLAB Central File Exchange. 检索来源 2022/9/1.

    若转载请保留以上file exchange链接及本文链接!!!


    水平阴影柱状图

    绘图方式基本和这篇所示一模一样:

    https://blog.csdn.net/slandarer/article/details/126641909

    水平柱状图示例

    只需要将'Horizontal'属性设置为,'on'即可:

    基本使用:

    y=[2 2 3 2 5; 2 5 6 2 5; 9 8 9 2 5];
    
    SH=shadowHist(y,'ShadowType',{'/','\','.','x','|'},'Horizontal','on');
    SH=SH.draw();
    
    • 1
    • 2
    • 3
    • 4

    含负值:

    y=[2 2 -3 -2 -5; -2 -5 6 2 5; 9 8 9 2 5];
    
    SH=shadowHist(y,'ShadowType',{'/','\','.','x','|'},'Horizontal','on');
    SH=SH.draw();
    
    • 1
    • 2
    • 3
    • 4

    堆叠:

    y=[2 2 -3 -2 -5; -2 -5 6 2 5; 9 8 9 2 5; -3 -5 6 2 5; 4 8 5 2 5];
    
    SH=shadowHist(y,'ShadowType',{'/','\','.','x','|'},'stacked','Horizontal','on');
    SH=SH.draw();
    
    • 1
    • 2
    • 3
    • 4

    添加图例:

    y=[2 2 3 2 5; 2 5 6 2 5; 9 8 9 2 5];
    
    SH=shadowHist(y,'ShadowType',{'/','\','.','_','+'},'Horizontal','on');
    SH=SH.draw();
    SH=SH.legend({'AAAAAAA','BBBBBBB','CCCCCCC','DDDDDDD','EEEEEEE'},'FontName','Arial','FontSize',11);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    全部阴影格式:

    y=[2 2 3 2 5 4 2 2 2 1;2 2 3 2 5 4 2 4 3 6];
    
    SH=shadowHist(y,'ShadowType',{'/','\','x','.','_','|','+','w','k','g'},'Horizontal','on');
    SH=SH.draw();
    SH=SH.legend({'A','B','C','D','E','F','G','H','I','J'});
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    工具函数完整代码

    classdef shadowHist
    % @author : slandarer
    % gzh  : slandarer随笔
    
    % 使用示例:
    % =========================================================================
    % y=[2 2 3 2 5; 2 5 6 2 5; 9 8 9 2 5];
    %
    % SH=shadowHist(y,'ShadowType',{'/','\','.','x','|'});
    % SH=SH.draw();
    
        properties
            ax         % 绘图坐标区域
            YData      % 数值矩阵
            shadowTypeList={'\','/','_','|','+','x','.','w','k','g'}
            shadowType % 阴影类型
            otherProp  % 其他初始属性
    
            oriBarHdl  % 原始图形句柄
            newBarHdl
            pshapeHdl  % polyshape图形句柄
            groupNum   % 组数
            classNum   % 类数
            YEndMat    % 末端y值
            maxY       % 最大Y值
            minY=0     % 最小Y值
            diffY      % Y值之差
            BarW       % 宽度
            YSep1      % linspace(obj.minY,obj.maxY+.02.*obj.diffY,obj.lineNum)
            YSep2
            XMean      % 每组柱状图每个柱子的中心位置
            lineNum=90 % 阴影基础线数量
    
            XYRate
            SHLegend
            oriLegend
        end
    
        methods
            function obj=shadowHist(varargin)
                % 基础属性设置
                if isa(varargin{1},'matlab.graphics.axis.Axes')
                    obj.ax=varargin{1};varargin(1)=[];
                else
                    obj.ax=gca;
                end
                hold(obj.ax,'on');
                obj.YData=varargin{1};varargin(1)=[];
                if any(strcmpi('shadowType',varargin))
                    tind=find(strcmpi('shadowType',varargin));
                    obj.shadowType=varargin{tind+1};
                    varargin([tind,tind+1])=[];
                else
                    obj.shadowType=obj.shadowTypeList;
                end
                obj.otherProp=varargin;
                help shadowHist
            end
    
            function obj=draw(obj)
                % 基础绘图
                obj.oriBarHdl=bar(obj.ax,obj.YData,obj.otherProp{:},'Tag','barBox');
                obj.XYRate=diff(obj.ax.YLim)./diff(obj.ax.XLim);
                % 更多属性获取
                obj.YEndMat=zeros([length(obj.oriBarHdl),length(obj.oriBarHdl(1).XData)]);
                for i=1:length(obj.oriBarHdl)
                    obj.YEndMat(i,:)=obj.oriBarHdl(i).YEndPoints;
                end
                obj.maxY=max(obj.YEndMat,[],[1,2]);
                obj.minY=min(0,min(obj.YEndMat,[],[1,2]));
                obj.diffY=obj.maxY-obj.minY;
    
                obj.classNum=length(obj.oriBarHdl);
                obj.groupNum=length(obj.oriBarHdl(1).XData);
    
                % 计算柱状图宽度和位置
                % obj.XMean=linspace(-obj.classNum+1,obj.classNum-1,obj.classNum)./(3+2*obj.classNum);
                for i=1:obj.classNum
                    obj.XMean(i)=obj.oriBarHdl(i).XEndPoints(1)-1;
                end
                tXMean=[obj.XMean,1];obj.BarW=(tXMean(2)-tXMean(1))./2.*obj.oriBarHdl(1).BarWidth;
                if strcmp(obj.oriBarHdl(1).BarLayout,'stacked')
                    obj.BarW=1./2.*obj.oriBarHdl(1).BarWidth;
                end
    %             if strcmp(obj.oriBarHdl(1).Horizontal,'on')
    %                 YY1=linspace(obj.oriBarHdl(1).XEndPoints(1),obj.oriBarHdl(end).XEndPoints(end)+2*obj.BarW,obj.lineNum)';
    %                 YY2=linspace(obj.oriBarHdl(1).XEndPoints(1)-2*obj.BarW,obj.oriBarHdl(end).XEndPoints(end),obj.lineNum)';
    %                 obj.YSep1=YY1(1)-YY2(2);obj.YSep2=YY1(2)-YY1(1);
    %             else
                    YY1=linspace(obj.minY,obj.maxY+.02.*obj.diffY,obj.lineNum)';
                    YY2=linspace(obj.minY-.02.*obj.diffY,obj.maxY,obj.lineNum)';
                    obj.YSep1=YY1(1)-YY2(2);obj.YSep2=YY1(2)-YY1(1);
    %             end
    
                % 循环绘制阴影
                for i=1:obj.classNum
                    obj.oriBarHdl(i).FaceColor='none';
                    obj.oriBarHdl(i).LineWidth=.8;
                    if strcmp(obj.oriBarHdl(1).BarLayout,'stacked')
                        obj.oriBarHdl(i).LineWidth=.8;
                    end
                    tType=mod(i-1,length(obj.shadowType))+1;
                    switch obj.shadowType{tType}
                        case 'w',obj.oriBarHdl(i).FaceColor=[1,1,1];
                        case 'k',obj.oriBarHdl(i).FaceColor=[0,0,0];
                        case 'g',obj.oriBarHdl(i).FaceColor=[.5,.5,.5];
                    end
                    for j=1:obj.groupNum
                        tX=[-1,1,1,-1].*obj.BarW+obj.XMean(i)+j;
                        tY=[obj.oriBarHdl(i).YEndPoints(j)-obj.oriBarHdl(i).YData(j),...
                            obj.oriBarHdl(i).YEndPoints(j)-obj.oriBarHdl(i).YData(j),...
                            obj.oriBarHdl(i).YEndPoints(j),...
                            obj.oriBarHdl(i).YEndPoints(j)];
                        if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                            tPolyPhape=polyshape(tY,tX);
                        else
                            tPolyPhape=polyshape(tX,tY);
                        end
                        switch obj.shadowType{tType}
                            case '\'
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    YY1=-1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    YY2=1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    XX1=linspace(obj.minY,obj.maxY+.02.*obj.diffY,obj.lineNum)';
                                    XX2=linspace(obj.minY-.02.*obj.diffY,obj.maxY,obj.lineNum)';
                                    tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                else
                                    XX1=-1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    XX2=1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    YY1=linspace(obj.minY,obj.maxY+.02.*obj.diffY,obj.lineNum)';
                                    YY2=linspace(obj.minY-.02.*obj.diffY,obj.maxY,obj.lineNum)';
                                    tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                end
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[XX1(k),YY1(k);XX2(k),YY2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end
                                plot(tXX(:),tYY(:),'Color',[0,0,0],'LineWidth',.5,'Tag','barShadow')
                                
                            case '/'
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    YY1=-1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    YY2=1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    XX1=linspace(obj.minY-.02.*obj.diffY,obj.maxY,obj.lineNum)';
                                    XX2=linspace(obj.minY,obj.maxY+.02.*obj.diffY,obj.lineNum)';
                                    tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                else
                                    XX1=-1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    XX2=1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    YY1=linspace(obj.minY-.02.*obj.diffY,obj.maxY,obj.lineNum)';
                                    YY2=linspace(obj.minY,obj.maxY+.02.*obj.diffY,obj.lineNum)';
                                    tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                end
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[XX1(k),YY1(k);XX2(k),YY2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end
                                plot(tXX(:),tYY(:),'Color',[0,0,0],'LineWidth',.5,'Tag','barShadow')
                            case '_'
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    tXX=linspace(tX(1),tX(2),5);
                                    if strcmp(obj.oriBarHdl(1).BarLayout,'stacked')
                                        tXX=linspace(tX(1),tX(2),9);
                                    end
                                    tXX=tXX(2:end-1);
                                    tYY=[tY(2),tY(3),nan]';
                                    tXX=repmat(tXX,[length(tYY),1]);
                                    tYY=repmat(tYY,[1,size(tXX,2)]);
                                    plot(tYY(:),tXX(:),'Color',[0,0,0],'LineWidth',.5,'Tag','barShadow')
                                else
                                    tXX=[tX(1),tX(2),nan];
                                    tYY=linspace(obj.minY-.02.*obj.diffY,obj.maxY+.02.*obj.diffY,obj.lineNum)';
                                    tYY(tYY>=max(tY))=[];tYY(tYY<=min(tY))=[];
                                    tXX=repmat(tXX,[length(tYY),1])';
                                    tYY=repmat(tYY,[1,size(tXX,1)])';
                                    plot(tXX(:),tYY(:),'Color',[0,0,0],'LineWidth',.5,'Tag','barShadow')
                                end
                            case '|'
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    tXX=[tX(1),tX(2),nan];
                                    tYY=linspace(obj.minY-.02.*obj.diffY,obj.maxY+.02.*obj.diffY,obj.lineNum)';
                                    tYY(tYY>=max(tY))=[];tYY(tYY<=min(tY))=[];
                                    tXX=repmat(tXX,[length(tYY),1])';
                                    tYY=repmat(tYY,[1,size(tXX,1)])';
                                    plot(tYY(:),tXX(:),'Color',[0,0,0],'LineWidth',.5,'Tag','barShadow')
                                else
                                    tXX=linspace(tX(1),tX(2),5);
                                    if strcmp(obj.oriBarHdl(1).BarLayout,'stacked')
                                        tXX=linspace(tX(1),tX(2),9);
                                    end
                                    tXX=tXX(2:end-1);
                                    tYY=[tY(2),tY(3),nan]';
                                    tXX=repmat(tXX,[length(tYY),1]);
                                    tYY=repmat(tYY,[1,size(tXX,2)]);
                                    plot(tXX(:),tYY(:),'Color',[0,0,0],'LineWidth',.5,'Tag','barShadow')
                                end
                            case '+'
                                tXX=[tX(1),tX(2),nan];
                                tYY=linspace(obj.minY-.02.*obj.diffY,obj.maxY+.02.*obj.diffY,obj.lineNum)';
                                tYY(tYY>=max(tY))=[];tYY(tYY<=min(tY))=[];
                                tXX1=repmat(tXX,[length(tYY),1])';
                                tYY1=repmat(tYY,[1,size(tXX1,1)])';
                                tXX=linspace(tX(1),tX(2),5);
                                if strcmp(obj.oriBarHdl(1).BarLayout,'stacked')
                                    tXX=linspace(tX(1),tX(2),9);
                                end
                                tXX=tXX(2:end-1);
                                tYY=[tY(2),tY(3),nan]';
                                tXX2=repmat(tXX,[length(tYY),1]);
                                tYY2=repmat(tYY,[1,size(tXX2,2)]);
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    plot([tYY1(:);nan;tYY2(:)],[tXX1(:);nan;tXX2(:)],'Color',[0,0,0],'LineWidth',.5,'Tag','barShadow')
                                else
                                    plot([tXX1(:);nan;tXX2(:)],[tYY1(:);nan;tYY2(:)],'Color',[0,0,0],'LineWidth',.5,'Tag','barShadow')
                                end
                            case 'x'
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    YY1=-1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    YY2=1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    XX1=linspace(obj.minY-obj.BarW.*2.4./obj.XYRate,obj.maxY,obj.lineNum)';
                                    XX2=XX1+obj.BarW.*2.4./obj.XYRate;
                                else
                                    XX1=-1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    XX2=1.2.*ones([obj.lineNum,1]).*obj.BarW+obj.XMean(i)+j;
                                    YY1=linspace(obj.minY-obj.BarW.*2.4.*obj.XYRate,obj.maxY,obj.lineNum)';
                                    YY2=YY1+obj.BarW.*2.4.*obj.XYRate;     
                                end
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[XX1(k),YY1(k);XX2(k),YY2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end;tXX1=tXX(:);tYY1=tYY(:);
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    XX1=linspace(obj.minY,obj.maxY+obj.BarW.*2.4./obj.XYRate,obj.lineNum)';
                                    XX2=XX1-obj.BarW.*2.4./obj.XYRate; 
                                else
                                    YY1=linspace(obj.minY,obj.maxY+obj.BarW.*2.4.*obj.XYRate,obj.lineNum)';
                                    YY2=YY1-obj.BarW.*2.4.*obj.XYRate;
                                end
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:obj.lineNum
                                    [in,~]=intersect(tPolyPhape,[XX1(k),YY1(k);XX2(k),YY2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end;tXX2=tXX(:);tYY2=tYY(:);
                                plot([tXX1(:);nan;tXX2(:)],[tYY1(:);nan;tYY2(:)],'Color',[0,0,0],'LineWidth',.5,'Tag','barShadow')
                            case '.'
                                tXX=linspace(tX(1)+obj.BarW./4,tX(2)-obj.BarW./4,3);
                                if strcmp(obj.oriBarHdl(1).BarLayout,'stacked')
                                    tXX=linspace(tX(1)+obj.BarW./8,tX(2)-obj.BarW./8,9);
                                end
                                tYY=linspace(obj.minY-.02.*obj.diffY,obj.maxY+.02.*obj.diffY,obj.lineNum)';
                                tYY(tYY>=max(tY))=[];tYY(tYY<=min(tY))=[];
                                tXX=repmat(tXX,[length(tYY),1]);
                                tYY=repmat(tYY,[1,size(tXX,2)]);
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    scatter(tYY(:),tXX(:),2,'filled','o','MarkerEdgeColor','none','MarkerFaceColor',[0,0,0],'Tag','barShadow')
                                else
                                    scatter(tXX(:),tYY(:),2,'filled','o','MarkerEdgeColor','none','MarkerFaceColor',[0,0,0],'Tag','barShadow')
                                end
                        end
                    end
                end
            end
    
            function obj=legend(obj,cellStr,varargin)
                obj.newBarHdl=bar(obj.ax,obj.YData,obj.otherProp{:},'FaceColor','none','EdgeColor','none');
                obj.oriLegend=legend(obj.newBarHdl,cellStr,varargin{:});
                obj.oriLegend.AutoUpdate='off';
                obj.oriLegend.Box='off';
                obj.ax.XLim=obj.ax.XLim;
                obj.ax.YLim=obj.ax.YLim;
    
                tXYMin=(obj.oriLegend.Position(1:2)-obj.ax.Position(1:2))./obj.ax.Position(3:4).*[diff(obj.ax.XLim),diff(obj.ax.YLim)]+[obj.ax.XLim(1),obj.ax.YLim(1)];
                tXYMax=(obj.oriLegend.Position(1:2)+obj.oriLegend.Position(3:4)-obj.ax.Position(1:2))./obj.ax.Position(3:4).*[diff(obj.ax.XLim),diff(obj.ax.YLim)]+[obj.ax.XLim(1),obj.ax.YLim(1)];
                boxHdl=fill([tXYMin(1),tXYMax(1),tXYMax(1),tXYMin(1)],[tXYMin(2),tXYMin(2),tXYMax(2),tXYMax(2)],[1,1,1],'Tag','lgdBox');
                
    
                for n=1:length((obj.oriBarHdl))
                    ttType=mod(n-1,length(obj.shadowType))+1;
                    squareHdl(n)=fill([0,0,0],[0,0,0],[1,1,1],'EdgeColor',[0,0,0],'LineWidth',.7,'Tag','barBox');
                    if strcmp(obj.shadowType{ttType},'.')
                        lgdLineHdl(n)=scatter(0,0,2,'filled','o','MarkerEdgeColor','none','MarkerFaceColor',[0,0,0],'Tag','barShadow');
                    else
                        lgdLineHdl(n)=plot(0,0,'Color',[0,0,0],'LineWidth',.5,'Tag','barBox','Tag','barShadow');
                    end
                end
                moveLgd()
                set(obj.ax.Parent,'WindowButtonMotionFcn',@moveLgd);
                function moveLgd(~,~)
                    XYMin=(obj.oriLegend.Position(1:2)-obj.ax.Position(1:2))./obj.ax.Position(3:4).*[diff(obj.ax.XLim),diff(obj.ax.YLim)]+[obj.ax.XLim(1),obj.ax.YLim(1)];
                    XYMax=(obj.oriLegend.Position(1:2)+obj.oriLegend.Position(3:4)-obj.ax.Position(1:2))./obj.ax.Position(3:4).*[diff(obj.ax.XLim),diff(obj.ax.YLim)]+[obj.ax.XLim(1),obj.ax.YLim(1)];
                    boxHdl.XData=[XYMin(1),XYMax(1),XYMax(1),XYMin(1)];
                    boxHdl.YData=[XYMin(2),XYMin(2),XYMax(2),XYMax(2)];
                    Y=XYMin(2)+(XYMax(2)-XYMin(2)).*linspace(1/length(obj.newBarHdl)/2+1/50,1-1/length(obj.newBarHdl)/2-1/50,length(obj.newBarHdl));
                    obj.XYRate=diff(obj.ax.YLim)./diff(obj.ax.XLim);
    
                    PBA=obj.ax.PlotBoxAspectRatio(2)./obj.ax.PlotBoxAspectRatio(1)./0.7896;
                    Y=fliplr(Y);
                    for i=1:length((obj.oriBarHdl))
                        tX=XYMin(1)+(XYMax(2)-XYMin(2))./2./length(obj.newBarHdl).*[1/10,1.18,1.18,1/10].*(PBA./obj.XYRate.*3);
                        tY=Y(i)+0.75.*[-1,-1,1,1].*(XYMax(2)-XYMin(2))./2./length(obj.newBarHdl);
                        squareHdl(i).XData=tX;
                        squareHdl(i).YData=tY;
                        tPolyPhape=polyshape(tX,tY);
                        tType=mod(i-1,length(obj.shadowType))+1;
                        switch obj.shadowType{tType}
                            case '\'
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    XX1=((-25:1:25).*obj.YSep2-obj.YSep1+tX(1))';
                                    XX2=((-25:1:25).*obj.YSep2+obj.YSep1+tX(2))';
                                    YY1=ones([51,1]).*(tY(2)-0.2.*(tY(3)-tY(2))./obj.XYRate./obj.XYRate);
                                    YY2=ones([51,1]).*(tY(3)+0.2.*(tY(3)-tY(2))./obj.XYRate./obj.XYRate);
                                else
                                    XX1=ones([51,1]).*(tX(1)-.2.*(tX(2)-tX(1)));
                                    XX2=ones([51,1]).*(tX(2)+.2.*(tX(2)-tX(1)));
                                    YY1=((-25:1:25).*obj.YSep2+obj.YSep1+Y(i))';
                                    YY2=((-25:1:25).*obj.YSep2-obj.YSep1+Y(i))';
                                end
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:51
                                    [in,~]=intersect(tPolyPhape,[XX1(k),YY1(k);XX2(k),YY2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end
                                lgdLineHdl(i).XData=tXX(:)';
                                lgdLineHdl(i).YData=tYY(:)';
                            case '/'
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    XX1=((-25:1:25).*obj.YSep2+obj.YSep1+tX(2))';
                                    XX2=((-25:1:25).*obj.YSep2-obj.YSep1+tX(1))';
                                    YY1=ones([51,1]).*(tY(2)-0.2.*(tY(3)-tY(2))./obj.XYRate./obj.XYRate);
                                    YY2=ones([51,1]).*(tY(3)+0.2.*(tY(3)-tY(2))./obj.XYRate./obj.XYRate);
                                else
                                    XX1=ones([51,1]).*(tX(1)-.2.*(tX(2)-tX(1)));
                                    XX2=ones([51,1]).*(tX(2)+.2.*(tX(2)-tX(1)));
                                    YY1=((-25:1:25).*obj.YSep2-obj.YSep1+Y(i))';
                                    YY2=((-25:1:25).*obj.YSep2+obj.YSep1+Y(i))';
                                end
                                tXX=zeros([3,obj.lineNum]);tYY=zeros([3,obj.lineNum]);
                                for k=1:51
                                    [in,~]=intersect(tPolyPhape,[XX1(k),YY1(k);XX2(k),YY2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end
                                lgdLineHdl(i).XData=tXX(:);
                                lgdLineHdl(i).YData=tYY(:);
                            case '_'
                                tXX=[tX(1),tX(2),nan];
                                tYY=linspace(tY(2),tY(3),5)';tYY=tYY(2:4);
                                tXX=repmat(tXX,[length(tYY),1])';
                                tYY=repmat(tYY,[1,size(tXX,1)])';
                                lgdLineHdl(i).XData=tXX(:);
                                lgdLineHdl(i).YData=tYY(:);
                            case '|'
                                tXX=linspace(tX(1),tX(2),11);tXX=tXX(2:end-1);
                                tYY=[tY(2),tY(3),nan]';
                                tXX=repmat(tXX,[length(tYY),1]);
                                tYY=repmat(tYY,[1,size(tXX,2)]);
                                lgdLineHdl(i).XData=tXX(:);
                                lgdLineHdl(i).YData=tYY(:);
                            case '+'
                                tXX=[tX(1),tX(2),nan];
                                tYY=linspace(tY(2),tY(3),5)';tYY=tYY(2:4);
                                tXX1=repmat(tXX,[length(tYY),1])';
                                tYY1=repmat(tYY,[1,size(tXX1,1)])';
                                tXX=linspace(tX(1),tX(2),11);tXX=tXX(2:end-1);
                                tYY=[tY(2),tY(3),nan]';
                                tXX2=repmat(tXX,[length(tYY),1]);
                                tYY2=repmat(tYY,[1,size(tXX2,2)]);
                                lgdLineHdl(i).XData=[tXX1(:);nan;tXX2(:)];
                                lgdLineHdl(i).YData=[tYY1(:);nan;tYY2(:)];
                            case 'x'
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    XX1=((-25:1:25).*obj.YSep2-obj.YSep1+tX(1))';
                                    XX2=XX1+((tY(3)+0.2.*(tY(3)-tY(2))./obj.XYRate)-(tY(2)-0.2.*(tY(3)-tY(2))./obj.XYRate))./obj.XYRate;
                                    YY1=ones([51,1]).*(tY(2)-0.2.*(tY(3)-tY(2))./obj.XYRate);
                                    YY2=ones([51,1]).*(tY(3)+0.2.*(tY(3)-tY(2))./obj.XYRate);
                                else
                                    XX1=ones([51,1]).*(tX(1)-.2.*(tX(2)-tX(1)));
                                    XX2=ones([51,1]).*(tX(2)+.2.*(tX(2)-tX(1)));
                                    YY1=((-25:1:25).*obj.YSep2-obj.YSep1+Y(i))';
                                    YY2=YY1+(tX(2)-tX(1)+.4.*(tX(2)-tX(1))).*obj.XYRate;
                                end
                                tXX=zeros([3,51]);tYY=zeros([3,51]);
                                for k=1:51
                                    [in,~]=intersect(tPolyPhape,[XX1(k),YY1(k);XX2(k),YY2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end;tXX1=tXX(:);tYY1=tYY(:);
                                if strcmp(obj.oriBarHdl(1).Horizontal,'on')
                                    XX1=((-25:1:25).*obj.YSep2+obj.YSep1+tX(2))';
                                    XX2=XX1-((tY(3)+0.2.*(tY(3)-tY(2))./obj.XYRate)-(tY(2)-0.2.*(tY(3)-tY(2))./obj.XYRate))./obj.XYRate;
                                else
                                    YY2=((-25:1:25).*obj.YSep2-obj.YSep1+Y(i))';
                                    YY1=YY2+(tX(2)-tX(1)+.4.*(tX(2)-tX(1))).*obj.XYRate;
                                end
                                tXX=zeros([3,51]);tYY=zeros([3,51]);
                                for k=1:51
                                    [in,~]=intersect(tPolyPhape,[XX1(k),YY1(k);XX2(k),YY2(k)]);
                                    if ~isempty(in)
                                        tXX(:,k)=[in(1,1);in(end,1);nan];
                                        tYY(:,k)=[in(1,2);in(end,2);nan];
                                    else
                                        tXX(:,k)=[nan;nan;nan];
                                        tYY(:,k)=[nan;nan;nan];
                                    end
                                end;tXX2=tXX(:);tYY2=tYY(:);
                                lgdLineHdl(i).XData=[tXX1(:);nan;tXX2(:)];
                                lgdLineHdl(i).YData=[tYY1(:);nan;tYY2(:)];
                            case '.'
                                tXX=linspace(tX(1),tX(2),9);
                                tYY=linspace(tY(2)+(tY(3)-tY(2))/6,tY(3)-(tY(3)-tY(2))/6,3)';
                                tXX(1)=[];tXX(end)=[];
                                tXX=repmat(tXX,[length(tYY),1]);
                                tYY=repmat(tYY,[1,size(tXX,2)]);
                                lgdLineHdl(i).XData=tXX(:);
                                lgdLineHdl(i).YData=tYY(:);
                            case 'w'
                                squareHdl(i).FaceColor=[1,1,1];
                            case 'k'
                                squareHdl(i).FaceColor=[0,0,0];
                            case 'g'
                                squareHdl(i).FaceColor=[.5,.5,.5];
                        end
                    end
                end
            end
        end
        % @author : slandarer
        % gzh  : slandarer随笔
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470

    未经允许本代码请勿作商业用途,引用的话可以引用我file exchange上的链接,可使用如下格式:

    Zhaoxu Liu (2022). Histogram with shadow 阴影柱状图 (https://www.mathworks.com/matlabcentral/fileexchange/117005-histogram-with-shadow), MATLAB Central File Exchange. 检索来源 2022/9/1.

    若转载请保留以上file exchange链接及本文链接!!!


    以上全部文件获取,
    链接:https://pan.baidu.com/s/1VGTCgcdlwdwm6P7mS1GrKg?pwd=slan

    提取码:slan

  • 相关阅读:
    12 正太总体参数的假设检验
    对比学习损失篇,从L-softmax到AM-softmax到circle-loss到PCCL
    .net8 blazor auto模式很爽(五)读取sqlite并显示(2)
    scanf导致程序运行时出现stack smashing
    CentOS 软件包 rpm 管理学习笔记
    LeetCode 热题 HOT 100 第八十天 494. 目标和 中等题 用python3求解
    C++ 多态
    线性代数笔记:三阶矩阵的特征值计算
    【云原生之Docker实战】使用docker部署PicUploader图床工具
    《Aerosol and Air Quality Research》期刊介绍(SCI 3区)
  • 原文地址:https://blog.csdn.net/slandarer/article/details/126654191