MATLAB 一行代码应用ggtheme主题--boxchart版(框须图,箱线图)

Posted slandarer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MATLAB 一行代码应用ggtheme主题--boxchart版(框须图,箱线图)相关的知识,希望对你有一定的参考价值。

这个比其他的难很多。。。
注: 至少需要 R2020a 版本才能使用,另外需要具有 Statistics and Machine Learning Toolbox 工具箱

1使用方式

假设你编写了如下程序:

x=randn(100,10);
boxchart(x)
legend('randn(100,10)')


加入一行:

x=randn(100,10);
boxchart(x)
legend('randn(100,10)')
ggThemeBox(gca,'flat_dark')


categorical 格式数组实例:

假设你编写了如下程序:

tbl = readtable('TemperatureData.csv');
monthOrder = 'January','February','March','April','May','June','July', ...
    'August','September','October','November','December';
tbl.Month = categorical(tbl.Month,monthOrder);
boxchart(tbl.Month,tbl.TemperatureF,'GroupByColor',tbl.Year)
ylabel('Temperature (F)')
legend


最后面加一行:

tbl = readtable('TemperatureData.csv');
monthOrder = 'January','February','March','April','May','June','July', ...
    'August','September','October','November','December';
tbl.Month = categorical(tbl.Month,monthOrder);
boxchart(tbl.Month,tbl.TemperatureF,'GroupByColor',tbl.Year)
ylabel('Temperature (F)')
legend
ggThemeBox(gca,'flat_dark');

2主题

主题有如下选择:
‘flat’/‘flat_dark’/‘camouflage’/‘chalk’/
‘copper’/‘dust’/‘earth’/‘fresh’/‘grape’/
‘grass’/‘greyscale’/‘light’/‘lilac’/‘pale’
‘sea’/‘sky’/‘solarized’
效果:
‘flat’

‘flat_dark’

‘camouflage’

‘chalk’

‘copper’

‘dust’

‘earth’

‘fresh’

‘grape’

‘grass’

‘greyscale’

‘light’

‘lilac’

‘pale’

‘sea’

‘sky’

‘solarized’

3完整代码

属性存到了.mat文件
文件下载地址:
链接:https://pan.baidu.com/s/1EMWVVzUCLP3RJIEK3Ljavg
提取码: ggtm

需要将.mat文件和函数放在同一个文件夹
完整代码:

function ax=ggThemeBox(varargin)
% @author:slandarer
% 
% 参数说明:
% -----------------------------------------------------
% AxesTheme   | 坐标区域风格       | 'flat'/'flat_dark'/'camouflage'/'chalk'/
%                                    'copper'/'dust'/'earth'/'fresh'/'grape'/
%                                    'grass'/'greyscale'/'light'/'lilac'/'pale'
%                                    'sea'/'sky'/'solarized'

% 获取要处理的坐标区域=====================================================
if strcmp(get(varargin1,'type'),'axes' )
    ax=varargin1;
else
    ax=gca;
end
hold(ax,'on')

% default==================================================================
theme.AxesTheme='flat';
if length(varargin)>1
    theme.AxesTheme=varargin2;
end

ax.Box='off';
ax.YGrid='on';
ax.XGrid='on';
ax.GridLineStyle='--';
ax.LineWidth=1.3;
% 主题风格化
Tm=load('themeCSS.mat');
Tm=Tm.theme;
ax.Color=Tm.(theme.AxesTheme).Color;
ax.TickLength=Tm.(theme.AxesTheme).TickLength;
ax.GridColorMode=Tm.(theme.AxesTheme).GridColorMode;
ax.GridColor=Tm.(theme.AxesTheme).GridColor;
ax.GridAlpha=Tm.(theme.AxesTheme).GridAlpha;
ax.XColor=Tm.(theme.AxesTheme).XColor;
ax.YColor=Tm.(theme.AxesTheme).YColor;
ax.TickDir=Tm.(theme.AxesTheme).TickDir;
ax.ColorOrder=Tm.(theme.AxesTheme).ColorOrder;

if ~isempty(ax.Legend)
    ax.Legend.Box='off';
    ax.Legend.FontSize=12;
    if mean(ax.Color)>0.6
        ax.Legend.TextColor=ax.XColor;
    else
        ax.Legend.TextColor=[0.9 0.9 0.9];
    end
    if ~isempty(regexpi(ax.Legend.Location,'out', 'once'))
        ax.Legend.TextColor=ax.XColor;
        ax.Legend.Title.FontSize=14;
    end
    ax.Legend.AutoUpdate='off';
end

n=1;
for i=length(ax.Children):-1:1
    ax.Children(i).Visible='on';
    ax.Children(i).WhiskerLineColor='none';
    ax.Children(i).BoxFaceColor=ax.ColorOrder(mod(n-1,size(ax.ColorOrder,1))+1,:);
    ax.Children(i).BoxFaceAlpha=1;
    axBp(i)=ax.Children(i);
    n=n+1;
end


Bw=axBp(1).BoxWidth;
Sw=1-Bw;
if length(axBp(1).XData(:))~=length(axBp(1).YData(:))
    n=1;
    for i=length(axBp):-1:1
        tDataX=axBp(i).XData;
        tDataY=axBp(i).YData;
        for j=1:length(axBp(i).XData)
            tjDataX=double(tDataX(j));
            fullDataY=tDataY(:,j);
            tjDataY=tDataY(:,j);
            if ~isempty(tjDataY)
                outliBool=isoutlier(tjDataY,'quartiles');
                outli=tjDataY(outliBool);
                scatter(ax,repmat(tjDataX,[sum(outliBool),1]),outli,45,'filled',...
                    'CData',Tm.(theme.AxesTheme).EdgeColor,'MarkerEdgeColor','none')
                
                tjDataY(outliBool)=[];
                plot(ax,[tjDataX,tjDataX],[max(tjDataY),min(tjDataY)],'LineWidth',1.2,...
                    'Color',Tm.(theme.AxesTheme).EdgeColor)
                
                qt25=quantile(fullDataY,0.25);
                qt75=quantile(fullDataY,0.75);
                fill(ax,tjDataX+Bw.*0.5.*[-1 1 1 -1],...
                    [qt25,qt25,qt75,qt75],ax.ColorOrder(mod(n-1,size(ax.ColorOrder,1))+1,:),...
                    'EdgeColor',Tm.(theme.AxesTheme).EdgeColor)
                
                med=median(fullDataY);
                plot(ax,[tjDataX-Bw.*0.5,tjDataX+Bw.*0.5],[med,med],'LineWidth',3,...
                    'Color',Tm.(theme.AxesTheme).EdgeColor)
            end
        end
        n=n+1;
    end
else
    n=1;
    nBw=Bw/length(axBp)/2;
    nSw=Sw/length(axBp)/2;
    for i=length(axBp):-1:1
        tDataX=axBp(i).XData;
        tDataY=axBp(i).YData;
        cgX=categories(tDataX);
        for j=1:length(cgX)
            fullDataY=tDataY(tDataX==cgXj);
            tjDataY=tDataY(tDataX==cgXj);
            if ~isempty(tjDataY)
                outliBool=isoutlier(tjDataY,'quartiles');
                outli=tjDataY(outliBool);
                tjDataX=j-(length(axBp)-1)*(nSw+nSw)+(length(axBp)-i)*2*(nSw+nSw);
                scatter(ax,repmat(tjDataX,[length(outli),1]),outli,45,'filled',...
                    'CData',Tm.(theme.AxesTheme).EdgeColor,'MarkerEdgeColor','none')
                
                tjDataY(outliBool)=[];
                plot(ax,[tjDataX,tjDataX],[max(tjDataY),min(tjDataY)],'LineWidth',1.2,...
                    'Color',Tm.(theme.AxesTheme).EdgeColor)
                
                qt25=quantile(fullDataY,0.25);
                qt75=quantile(fullDataY,0.75);
                fill(ax,tjDataX+nBw.*[-1 1 1 -1],...
                    [qt25,qt25,qt75,qt75],ax.ColorOrder(mod(n-1,size(ax.ColorOrder,1))+1,:),...
                    'EdgeColor',Tm.(theme.AxesTheme).EdgeColor)
                
                med=median(fullDataY);
                plot(ax,[tjDataX-nBw,tjDataX+nBw],[med,med],'LineWidth',3,...
                    'Color',Tm.(theme.AxesTheme).EdgeColor)
            end
        end
        n=n+1;
    end
end

end

以上是关于MATLAB 一行代码应用ggtheme主题--boxchart版(框须图,箱线图)的主要内容,如果未能解决你的问题,请参考以下文章

MATLAB 一行代码应用ggtheme主题--plot版

MATLAB 一行代码应用ggtheme主题--plot版

MATLAB 一行代码应用ggtheme主题--plot版

MATLAB 一行代码应用ggtheme主题--plot版

MATLAB 一行代码应用ggtheme主题--density版

MATLAB 一行代码应用ggtheme主题--density版