失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 如何使用MATLAB绘制ggplot风格图片(散点图及折线图)

如何使用MATLAB绘制ggplot风格图片(散点图及折线图)

时间:2024-07-04 16:20:07

相关推荐

如何使用MATLAB绘制ggplot风格图片(散点图及折线图)

写了一个用来修饰MATLAB AXES的函数,目前只支持散点图及折线图,之后可能会再出曲面修饰函数或者柱状图修饰函数之类的一系列修饰器,效果如下:

0使用效果

1参数说明

程序主要有以下几个参数:

ax| 程序的第一个参数,即要作用的AXES区域,如果设置为[]则自动获取gca作为ax。AxesTheme| 坐标区域风格,可设置为:‘gray’/‘economist’/‘wsj’/‘own1’ColorOrder| 图形对象颜色序列,可设置为:‘default’/‘none’/‘npg’/‘lancet’/‘starterk’/‘Set1’/‘Set2’/‘Set3’/‘Dark2’/‘own1’LegendStyle| 图例样式,可设置为:‘ggplot’/‘own1’EdgeStyle| 轮廓样式,可设置为:‘none’/‘gray’/‘white’/‘ori’

2基本使用

假设你编写了如下程序:

t=0:0.1:3*pi;plot(t,sin(t))hold onplot(t,cos(t./2))plot(t,t)lgd=legend('y=sin(t)','y=cos(t/2)','y=t');lgd.Location='northwest';title(lgd,'Func','FontSize',12)

则绘制图像如下:

若在程序最后加上一行:

t=0:0.1:3*pi;plot(t,sin(t))hold onplot(t,cos(t./2))plot(t,t)lgd=legend('y=sin(t)','y=cos(t/2)','y=t');lgd.Location='northwest';title(lgd,'Func','FontSize',12)% 修饰用代码ggplotAxes2D([]);

则结果如下:

这里啥参数都没设,用的就都是默认值。

若将最后一句改为:

ggplotAxes2D([],'ColorOrder','Set2');

就能使用Set2风格的配色:

3AXES及图例风格

AxesTheme : gray

LegendStyle : ggplot

ggplotAxes2D([],'AxesTheme','gray','LegendStyle','ggplot','ColorOrder','Set2');

我们发现绘图效果不变,说明这俩是默认值

AxesTheme : economist

LegendStyle : ggplot

ggplotAxes2D([],'AxesTheme','economist','LegendStyle','ggplot','ColorOrder','Set2');

注,若通过该按钮保存则可把背景颜色一同保存:

AxesTheme : wsj

LegendStyle : ggplot

ggplotAxes2D([],'AxesTheme','wsj','LegendStyle','ggplot','ColorOrder','Set2');

AxesTheme : own1

LegendStyle : own1

ggplotAxes2D([],'AxesTheme','own1','LegendStyle','own1','ColorOrder','Set2');

4边缘风格

散点图边缘风格

假设编写以下散点图代码:

mu = [2 3];SIGMA = [1 0; 0 2];r = mvnrnd(mu,SIGMA,100);scatter(r(:,1),r(:,2),'filled');hold on;mu = [6 7];SIGMA = [ 1 0; 0 2];r2 = mvnrnd(mu,SIGMA,100);scatter(r2(:,1),r2(:,2),'filled')mu = [8 9];SIGMA = [ 1 0; 0 1];r3 = mvnrnd(mu,SIGMA,100);scatter(r3(:,1),r3(:,2),'filled')lgd=legend('scatter1','scatter2','scatter3');lgd.Location='northwest';

运行效果如下:

我们在用修饰器时加入EdgeStyle属性:

EdgeStyle : white

ggplotAxes2D([],'ColorOrder','Set2','EdgeStyle','white');

EdgeStyle : gray

ggplotAxes2D([],'ColorOrder','Set2','EdgeStyle','gray');

EdgeStyle : ori

ori即为边缘使用图形对象原色,但是亮度降低。

ggplotAxes2D([],'ColorOrder','Set2','EdgeStyle','ori');

折线边缘风格

假设我们编写了如下代码:

t=0:0.35:3*pi;plot(t,sin(t),'Marker','d')hold onplot(t,cos(t./2),'Marker','o')plot(t,t,'Marker','^')lgd=legend('y=sin(t)','y=cos(t/2)','y=t');lgd.Location='northwest';title(lgd,'Func','FontSize',12)

我们也可对其进行一系列修饰,例如:

ggplotAxes2D([],'AxesTheme','wsj','ColorOrder','Set2','EdgeStyle','gray');

ggplotAxes2D([],'AxesTheme','own1','ColorOrder','own1','EdgeStyle','gray','LegendStyle','own1');

5完整代码

function ax=ggplotAxes2D(varargin)%% @author:slandarer% % 参数说明:% -----------------------------------------------------% AxesTheme | 坐标区域风格 | 'gray'/'economist'/'wsj'/'own1'% ColorOrder | 图形对象颜色序列 | 'default'/'none'/'npg'/'lancet'/'starterk'% 'Set1'/'Set2'/'Set3'/'Dark2'/'own1'% LegendStyle | 图例样式 | 'ggplot'/'own1'% EdgeStyle | 轮廓样式 | 'none'/'gray'/'white'/'ori'% ax.Legend.UserData.NewBkg 图例新背景% ax.Legend.UserData.NewTitle 图例新标题% ax.UserData.NewYTick(i)Y轴新标签% 获取要处理的坐标区域=====================================================if strcmp(get(varargin{1},'type'),'axes' )ax=varargin{1};elseax=gca;endhold(ax,'on')% default==================================================================theme.AxesTheme='gray';theme.ColorOrder='default';theme.LegendStyle='ggplot';theme.EdgeStyle='none';%从可变长度变量中提取有用信息==============================================for i=1:length(varargin)tempVar=varargin{i};if strcmp(tempVar,'AxesTheme')||strcmp(tempVar,'axesTheme')||strcmp(tempVar,'axestheme')theme.AxesTheme=varargin{i+1};endif strcmp(tempVar,'ColorOrder')||strcmp(tempVar,'colorOrder')||strcmp(tempVar,'colororder')theme.ColorOrder=varargin{i+1};endif strcmp(tempVar,'LegendStyle')||strcmp(tempVar,'legendStyle')||strcmp(tempVar,'legendstyle')theme.LegendStyle=varargin{i+1};endif strcmp(tempVar,'EdgeStyle')||strcmp(tempVar,'edgeStyle')||strcmp(tempVar,'edgestyle')theme.EdgeStyle=varargin{i+1};endend% 配色方案switch theme.ColorOrdercase 'none'case 'default'ax.ColorOrder=[0.9900 0.4500 0.45000.8500 0.5600 00.6400 0.6500 00.2200 0.7100 00 0.7500 0.49000 0.7500 0.77000 0.6900 0.96000.5800 0.5600 1.00000.9100 0.4200 0.95001.0000 0.3800 0.7400]; case 'npg'ax.ColorOrder=[0.9000 0.2900 0.21000.3000 0.7300 0.84000 0.6300 0.53000.2400 0.3300 0.53000.9500 0.6100 0.50000.5200 0.5700 0.71000.5700 0.8200 0.76000.8600 0 00.4900 0.3800 0.28000.6900 0.6100 0.5200];case 'lancet'ax.ColorOrder=[0 0.2700 0.55000.9300 0 00.2600 0.7100 0.25000 0.6000 0.71000.5700 0.3700 0.62000.9900 0.6900 0.57000.6800 0 0.16000.6800 0.7100 0.71000.1100 0.1000 0.1000];case 'starterk'ax.ColorOrder=[0.8000 0.0500 00.3600 0.5300 0.85000.5200 0.7400 01.0000 0.8000 00.4900 0.5300 0.56000 0.7100 0.89000 0.6900 0.4000];case 'Set1'ax.ColorOrder=[0.8900 0.1000 0.11000.2200 0.4900 0.72000.3000 0.6900 0.29000.6000 0.3100 0.64001.0000 0.5000 01.0000 1.0000 0.20000.6500 0.3400 0.16000.9700 0.5100 0.75000.6000 0.6000 0.6000];case 'Set2'ax.ColorOrder=[0.4000 0.7600 0.65000.9900 0.5500 0.38000.5500 0.6300 0.80000.9100 0.5400 0.76000.6500 0.8500 0.33001.0000 0.8500 0.18000.9000 0.7700 0.58000.7000 0.7000 0.7000];case 'Set3'ax.ColorOrder=[0.5500 0.8300 0.78001.0000 1.0000 0.70000.7500 0.7300 0.85000.9800 0.5000 0.45000.5000 0.6900 0.83000.9900 0.7100 0.38000.7000 0.8700 0.41000.9900 0.8000 0.90000.8500 0.8500 0.85000.7400 0.5000 0.74000.8000 0.9200 0.77000.8300 0.8300 0.8300];case 'Dark2'ax.ColorOrder=[0.1100 0.6200 0.47000.8500 0.3700 0.01000.4600 0.4400 0.70000.9100 0.1600 0.54000.4000 0.6500 0.12000.9000 0.6700 0.01000.6500 0.4600 0.11000.4000 0.4000 0.4000];case 'own1'ax.ColorOrder=[0.8500 0.7100 0.80000.3700 0.4400 0.66000.7500 0.6900 0.83000.3700 0.2200 0.52000.8400 0.2500 0.55000.7200 0.5200 0.52000.6100 0.3800 0.60000.0400 0.1400 0.28001.0000 0.5800 0.35000.9500 0.8900 0.7500];end% 部分plot scatter修饰if falsechildrenNum=length(ax.Children);for i=1:childrenNumswitch theme.EdgeStylecase 'none'EdgeColor=[];case 'gray'EdgeColor=[0.3 0.3 0.3];case 'white'EdgeColor=[0.96 0.96 0.96];case 'ori'EdgeColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:).*0.5;endswitch get(ax.Children(i),'type')case 'line'ax.Children(i).LineWidth=1.8;if ~isempty(EdgeColor)ax.Children(i).LineWidth=1.5;ax.Children(i).MarkerEdgeColor=EdgeColor;ax.Children(i).MarkerSize=8;ax.Children(i).MarkerFaceColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);endcase 'scatter'if ~isempty(EdgeColor)ax.Children(i).MarkerFaceColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);ax.Children(i).LineWidth=1.5;ax.Children(i).MarkerEdgeColor=EdgeColor;ax.Children(i).SizeData=60;endendendendif truechildrenNum=length(ax.Children);lineSet=[];scatterSet=[];n=1;for i=childrenNum:-1:1if strcmp(get(ax.Children(i),'type'),'scatter')scatterSet{n}=ax.Children(i);n=n+1;endendn=1;for i=childrenNum:-1:1if strcmp(get(ax.Children(i),'type'),'line')lineSet{n}=ax.Children(i);n=n+1;endendfor i=1:length(scatterSet)switch theme.EdgeStylecase 'none'EdgeColor=[];case 'gray'EdgeColor=[0.3 0.3 0.3];case 'white'EdgeColor=[0.96 0.96 0.96];case 'ori'EdgeColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:).*0.5;endscatterSet{i}.MarkerEdgeColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);if ~isempty(EdgeColor)scatterSet{i}.LineWidth=1.5;scatterSet{i}.MarkerEdgeColor=EdgeColor;scatterSet{i}.SizeData=60;scatterSet{i}.MarkerFaceColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);endendfor i=1:length(lineSet)switch theme.EdgeStylecase 'none'EdgeColor=[];case 'gray'EdgeColor=[0.3 0.3 0.3];case 'white'EdgeColor=[0.96 0.96 0.96];case 'ori'EdgeColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:).*0.5;endlineSet{i}.LineWidth=1.8;lineSet{i}.Color=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);if ~isempty(EdgeColor)lineSet{i}.LineWidth=1.5;lineSet{i}.MarkerEdgeColor=EdgeColor;lineSet{i}.MarkerSize=8;lineSet{i}.MarkerFaceColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);endendend% legend 风格化if ~isempty(ax.Legend)switch theme.LegendStylecase 'ggplot'ax.Legend.FontSize=11;ax.Legend.Title.FontSize=14;ax.Legend.AutoUpdate='off';if ~isempty(regexpi(ax.Legend.Location,'out'))ax.Legend.Box='off';lgdPos=ax.Legend.Position;xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...(lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...(lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];ax.Legend.Title.Visible='off';xyMin(1),xyMax(2)ax.Legend.UserData.NewTitle=text(ax,xyMin(1),xyMax(2),[' ',ax.Legend.Title.String],...'FontSize',14,'VerticalAlignment','top','FontWeight','bold');elseax.Legend.Box='off';lgdPos=ax.Legend.Position;xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...(lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...(lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];xDiff=(xyMax(1)-xyMin(1));yDiff=(xyMax(2)-xyMin(2));ax.Legend.UserData.NewBkg=rectangle(ax,'Position',[xyMin,xDiff,yDiff],'Curvature',0.2,...'LineWidth',1.2,'EdgeColor',[0.39 0.41 0.39],'FaceColor',[1 1 1 .2]);%ax.Legend.Title.FontSize=14;ax.Legend.Title.Visible='off';ax.Legend.UserData.NewTitle=text(ax,xyMin(1),xyMax(2),[' ',ax.Legend.Title.String],...'FontSize',14,'VerticalAlignment','top','FontWeight','bold');endcase 'own1'ax.Legend.Color=[0.9412 0.9412 0.9412];ax.Legend.LineWidth=0.8;ax.Legend.FontSize=11;endend% axes风格化switch theme.AxesThemecase 'gray'ax.Parent.Color=[1 1 1];ax.Color=[0.9,0.9,0.9];ax.Box='off';grid(ax,'on');ax.TickDir='out';ax.GridColor=[1 1 1];ax.GridAlpha=1;ax.LineWidth=1.2;ax.XColor=[0.33,0.33,0.33];ax.YColor=[0.33,0.33,0.33];ax.TickLength=[0.015 0.025];plot(ax,[ax.XLim(2),ax.XLim(1),ax.XLim(1),ax.XLim(2),ax.XLim(2)],...[ax.YLim(2),ax.YLim(2),ax.YLim(1),ax.YLim(1),ax.YLim(2)],...'Color',[1 1 1],'LineWidth',2)case 'economist'ax.Parent.Color=[0.8400 0.8900 0.9200];ax.Color=[0.8400 0.8900 0.9200];ax.Parent.InvertHardcopy='off';ax.Box='off';ax.YGrid='on';ax.GridColor=[1 1 1];ax.GridAlpha=1;ax.LineWidth=1.2;ax.XColor=[0.33,0.33,0.33];ax.YColor='none';ax.TickLength=[0.015 0.025];for i=1:length(ax.YTick)ax.UserData.NewYTick(i)=...text(ax,ax.XLim(1)-ax.TickLength(1)/(ax.Position(3))*(ax.XLim(2)-ax.XLim(1)),...ax.YTick(i),ax.YTickLabel{i},'HorizontalAlignment','right','Color',[0.33,0.33,0.33]);endcase 'wsj'ax.Parent.Color=[0.9700 0.9500 0.8900];ax.Color=[0.9700 0.9500 0.8900];ax.Parent.InvertHardcopy='off';ax.Box='off';ax.YGrid='on';ax.GridAlpha=1;ax.LineWidth=0.8;ax.YColor='none';ax.GridLineStyle=':';ax.TickLength=[0.015 0.025];for i=1:length(ax.YTick)ax.UserData.NewYTick(i)=...text(ax,ax.XLim(1)-ax.TickLength(1)/(ax.Position(3))*(ax.XLim(2)-ax.XLim(1)),...ax.YTick(i),ax.YTickLabel{i},'HorizontalAlignment','right','Color',[0.33,0.33,0.33]);endcase 'own1'grid(ax,'on');ax.GridLineStyle='--';ax.LineWidth = 1;endend

6注

如果对AXES形状不满意,请调整到合适的形状后再命令行调用函数scatter和plot可以同时修饰例如:

如果觉得《如何使用MATLAB绘制ggplot风格图片(散点图及折线图)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。