本文主要是介绍MATLAB 一行代码应用ggtheme主题--plot版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1使用方式
假设你画了如下曲线:
t=0:0.6:3*pi;
plot(t,sin(t).*1.2,'LineWidth',2,'Marker','o')
hold on
plot(t,cos(t./2),'LineWidth',2,'Marker','s')
plot(t,t,'LineWidth',2,'Marker','^')lgd=legend(' y=1.2sin(t)',' y=cos(t/2)',' y=t');
lgd.Location='best';
title(lgd,'Func')
在最后面加上一行变为:
t=0:0.6:3*pi;
plot(t,sin(t).*1.2,'LineWidth',2,'Marker','o')
hold on
plot(t,cos(t./2),'LineWidth',2,'Marker','s')
plot(t,t,'LineWidth',2,'Marker','^')lgd=legend(' y=1.2sin(t)',' y=cos(t/2)',' y=t');
lgd.Location='best';
title(lgd,'Func')ggThemePlot(gca,'dust');
这份代码和之前ggplot风格图片修饰器类似,区别的是该函数只对plot函数修饰,主题更多,之后会写柱状图版,密度图版及框须图版
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=ggThemePlot(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')
for i=1:length(ax.Children)if strcmp(get(ax.Children(i),'type'),'line')ax.Children(i).ColorMode='auto';ax.Children(i).MarkerSize=10;end
end% default==================================================================
theme.AxesTheme='flat';
if length(varargin)>1theme.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.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;end
endn=1;
for i=length(ax.Children):-1:1if strcmp(get(ax.Children(i),'type'),'line')ax.Children(i).MarkerFaceColor=ax.ColorOrder(mod(n-1,size(ax.ColorOrder,1))+1,:);ax.Children(i).Color=ax.ColorOrder(mod(n-1,size(ax.ColorOrder,1))+1,:);ax.Children(i).MarkerEdgeColor=ax.Color;n=n+1;end
endend
这篇关于MATLAB 一行代码应用ggtheme主题--plot版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!