您当前的位置: 首页 >  ar

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

发布时间:2021-09-15 08:06:39 ,浏览量:5

好家伙我发现之前写的作用于plot的可以直接用,只不过就是多了一点柱状图外框而已,不过想画出更加贴近ggplot风格的图,可以用最后的代码

1使用方式

假设你编写了如下柱状图:

y=[6 3 4 2 1;12 6 3 2 1;9 5 2 2 2;7 3 1 1 0;5 2 2 1 1;2 1 1 0 1;]; bar(y) legend('class1','class2','class3','class4','class5') 


加一行:

ggThemeBar(gca,'dust'); 


展示一下两种排列方式:

y=[6 3 4 2 1;12 6 3 2 1;9 5 2 2 2;7 3 1 1 0;5 2 2 1 1;2 1 1 0 1;]; subplot(1,2,1) bar(y) legend('class1','class2','class3','class4','class5') ggThemeBar(gca,'dust'); subplot(1,2,2) bar(y,'stacked') legend('class1','class2','class3','class4','class5') ggThemeBar(gca,'dust'); 

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=ggThemeBar(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(varargin{1},'type'),'axes' ) ax=varargin{1}; else ax=gca; end hold(ax,'on') % default================================================================== theme.AxesTheme='flat'; if length(varargin)>1 theme.AxesTheme=varargin{2}; end ax.Box='off'; ax.YGrid='on'; ax.XGrid='on'; ax.GridLineStyle='--'; ax.LineWidth=1.2; % 主题风格化 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
end

vermat=version('-release'); vermat=str2double(vermat(1:4))+abs(vermat(5))-97.5; tindex=1; for i=length(ax.Children):-1:1 if strcmp(get(ax.Children(i),'type'),'bar') ax.Children(i).EdgeColor='none'; if vermat<2019.5 ax.Children(i).FaceColor=ax.ColorOrder(tindex,:); end
    end
    tindex=mod(tindex,size(ax.ColorOrder,1))+1; end
end
关注
打赏
查看更多评论

暂无认证

  • 5浏览

    0关注

    115983博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录