本文主要是介绍MATLAB 一行代码应用ggtheme主题--density版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1使用方式
假设你画了如下概率密度曲线:
x=linspace(-8,12,100);
y1=normpdf(x,4,6);
y2=normpdf(x,0,1).*0.5+normpdf(x,4,2).*0.5;
y3=normpdf(x,-3,2);
plot(x,y1);
hold on
plot(x,y2);
plot(x,y3);
ax=gca;
ax.XLim=[-8,12];
legend('density1','density2','density3')
在最后加上一行:
ggThemeDensity(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=ggThemeDensity(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};
elseax=gca;
end
hold(ax,'on')% default==================================================================
theme.AxesTheme='flat';
if length(varargin)>1theme.AxesTheme=varargin{2};
endax.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;
ax.XLim=ax.XLim;
ax.YLim=ax.YLim;if ~isempty(ax.Legend)tStr=ax.Legend.String;
endfor i=length(ax.Children):-1:1axDS(i)=ax.Children(i);
end
n=1;
for i=length(axDS):-1:1if strcmp(get(axDS(i),'type'),'line')tXData=axDS(i).XData;tYData=axDS(i).YData;if tXData(1)>tXData(end)tXData=tXData(end:-1:1);tYData=tYData(end:-1:1);endtXData=[min(tXData),tXData,max(tXData)];tYData=[0,tYData,0];fill(ax,tXData,tYData,ax.ColorOrder(mod(n-1,size(ax.ColorOrder,1))+1,:),...'LineWidth',1.4,'EdgeColor',ax.ColorOrder(mod(n-1,size(ax.ColorOrder,1))+1,:),...'FaceAlpha',0.7);n=n+1;end
end
for i=length(axDS):-1:1delete(axDS(i));
endif ~isempty(ax.Legend)ax.Legend.Box='off';ax.Legend.FontSize=12;if mean(ax.Color)>0.6ax.Legend.TextColor=ax.XColor;elseax.Legend.TextColor=[0.9 0.9 0.9];endif ~isempty(regexpi(ax.Legend.Location,'out', 'once'))ax.Legend.TextColor=ax.XColor;ax.Legend.Title.FontSize=14;endax.Legend.String=tStr;
endend
这篇关于MATLAB 一行代码应用ggtheme主题--density版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!