Matlab Gramm绘图工具箱

简介

Gramm是Matlab的数据可视化工具箱,可以轻松灵活地生成具有出版质量的图表。Gramm无需循环绘制颜色或子图,可自动生成颜色和图例,处理轴限制等。Gramm一大优势就是进行数据统计和对比,尤其是对比。偶然间发现这个超级有用的绘图工具箱,便记录一下,后续会陆续补充使用技巧。下载链接:https://github.com/piermorel/gramm。安装在github上也有介绍,很简单。

官方例子

函数:https://github.com/piermorel/gramm/blob/master/gramm%20cheat%20sheet.pdf
例子:https://htmlpreview.github.io/?https://github.com/piermorel/gramm/blob/master/html/examples.html
Matlab论坛例子:https://www.mathworks.com/matlabcentral/fileexchange/54465-gramm-complete-data-visualization-toolbox-ggplot2-r-like

工作流程

Gramm典型工作流程如下:首先,为gramm提供有关图形的相关数据:X和Y变量,以及将确定颜色,子图行/列等的分组变量;然后,在图形中添加图形层,每一条指令可添加一层,所有层都提供许多自定义选项。

例子1

新车的燃油经济性随时间的变化,不同颜色表示汽缸数(即,用气缸数进行数据分类)。

clc; clear all; close all
load carbig.mat %Load example dataset about cars
origin_region=num2cell(org,2); %Convert origin data to a cellstr

% Create a gramm object, provide x (year of production) and y (fuel economy) data,
% color grouping data (number of cylinders) and select a subset of the data
% x 年限数据,y 燃油经济率,利用气缸数进行分类
g=gramm('x',Model_Year,'y',MPG,'color',Cylinders,'subset',Cylinders~=3 & Cylinders~=5)
% Subdivide the data in subplots horizontally by region of origin
% 不同国家进行进行子图绘制
g.facet_grid([],origin_region)
% Plot raw data as points
g.geom_point()
% Plot linear fits of the data with associated confidence intervals
g.stat_glm()
% Set appropriate names for legends
% 画xlabel ylabel,以及图例
g.set_names('column','Origin','x','Year of production','y','Fuel economy (MPG)','color','# Cylinders')
%Set figure title
g.set_title('Fuel economy of new cars between 1970 and 1982')
% Do the actual drawing
g.draw()
g.export('file_name','my_figure','export_path','C:/Users/Administrator/Desktop/','file_type','png')

在这里插入图片描述

例子2

clc; clear all; close all
load example_data;
clear g
% 指定x y 数据,并且排除气缸数为35的数据
g(1,1)=gramm('x',cars.Horsepower,'y',cars.MPG,'subset',cars.Cylinders~=3 & cars.Cylinders~=5);
% 绘制散点图
g(1,1).geom_point();
g(1,1).set_names('x','Horsepower','y','MPG');
g(1,1).set_title('No groups');

% 指定x y 数据,并且排除气缸数为35的数据,根据气缸数绘制图例和赋予不同颜色
g(1,2)=gramm('x',cars.Horsepower,'y',cars.MPG,'subset',cars.Cylinders~=3 & cars.Cylinders~=5,'color',cars.Cylinders);
g(1,2).geom_point();
g(1,2).set_names('x','Horsepower','y','MPG','color','# Cyl');
g(1,2).set_title('color');

% 根据气缸数绘制图例和赋予不同颜色亮度
g(1,3)=gramm('x',cars.Horsepower,'y',cars.MPG,'subset',cars.Cylinders~=3 & cars.Cylinders~=5,'lightness',cars.Cylinders);
g(1,3).geom_point();
g(1,3).set_names('x','Horsepower','y','MPG','lightness','# Cyl');
g(1,3).set_title('lightness');

% 根据气缸数绘制图例和赋予不同大小
g(2,1)=gramm('x',cars.Horsepower,'y',cars.MPG,'subset',cars.Cylinders~=3 & cars.Cylinders~=5,'size',cars.Cylinders);
g(2,1).geom_point();
g(2,1).set_names('x','Horsepower','y','MPG','size','# Cyl');
g(2,1).set_title('size');

% 根据气缸数绘制图例和赋予不同标记
g(2,2)=gramm('x',cars.Horsepower,'y',cars.MPG,'subset',cars.Cylinders~=3 & cars.Cylinders~=5,'marker',cars.Cylinders);
g(2,2).geom_point();
g(2,2).set_names('x','Horsepower','y','MPG','marker','# Cyl');
g(2,2).set_title('marker');

% 根据气缸数绘制图例和赋予不同线形
g(2,3)=gramm('x',cars.Horsepower,'y',cars.MPG,'subset',cars.Cylinders~=3 & cars.Cylinders~=5,'linestyle',cars.Cylinders);
g(2,3).geom_line();
g(2,3).set_names('x','Horsepower','y','MPG','linestyle','# Cyl');
g(2,3).set_title('linestyle');

g(3,1)=gramm('x',cars.Horsepower,'y',cars.MPG,'subset',cars.Cylinders~=3 & cars.Cylinders~=5);
% 根据气缸数绘制子图,垂直分布
g(3,1).facet_grid(cars.Cylinders,[]);
g(3,1).geom_point();
g(3,1).set_names('x','Horsepower','y','MPG','row','# Cyl');
g(3,1).set_title('subplot rows');

g(3,2)=gramm('x',cars.Horsepower,'y',cars.MPG,'subset',cars.Cylinders~=3 & cars.Cylinders~=5);
% 根据气缸数绘制子图,水平分布
g(3,2).facet_grid([],cars.Cylinders);
g(3,2).geom_point();
g(3,2).set_names('x','Horsepower','y','MPG','column','# Cyl');
g(3,2).set_title('subplot columns');

figure('Position',[100 100 800 800]);
g.draw();

g.export('file_name','my_figure','export_path','C:/Users/Administrator/Desktop/','file_type','png')

在这里插入图片描述

例子3

设置颜色

clear g5
x = randn(50);
g5=gramm('x',x);
g5.stat_bin('geom','stacked_bar');
g5.set_title('''overlaid_bar''');
% 这里的颜色是颜色图中的第一个颜色
g5.set_color_options('map','brewer_paired');
figure('Position',[100 100 400 300]);
g5.draw();
% 设置边框,还有很多属性可以自行设置
% 暴力设置
% g5.facet_axes_handles.Box = 1;
% 推荐方法,通过轴属性
g.axe_property('Box',1);

g5.export('file_name','my_figure','export_path','C:/Users/Administrator/Desktop/','file_type','png')

在这里插入图片描述

例子4

利用句柄进行添加text属性

clear g
g=gramm('x',counts);
g.stat_bin('fill','face');
g.set_color_options('map','brewer_paired');
g.set_names('x','Data volume','y','Number of sites');
figure('Position',[100 100 400 300]);

g.draw();
g.facet_axes_handles.Box = 1;
text(10000,500,['N = ' num2str(size(lat,1))],'Parent',g.facet_axes_handles(1));

在这里插入图片描述

例子5

轴属性设置,设置ticklabel,并进行旋转

load example_data;

clear g
%Compute number of models outside of gramm so that the output can be used
%as label
temp_table=rowfun(@numel,cars_table,'OutputVariableNames','N','GroupingVariables',{
    
    'Origin_Region','Model_Year'},'InputVariables','MPG');

x = temp_table.Model_Year(1:13);
y = temp_table.N(1:13);
l = temp_table.N(1:13);

g=gramm('x',x,'y',y);
g.geom_bar('LineWidth',0.5);
g.geom_label('VerticalAlignment','bottom','HorizontalAlignment','center');
g.set_names('x','Year','y','Number of models');
figure('Position',[100 100 400 300]);
g.axe_property('XTickLabelRotation',60,'XTick',x,'XTickLabel',y,'Box',1);

g.set_color_options('map','brewer_paired');
g.draw();

g.export('file_name','my_figure','export_path','C:/Users/Administrator/Desktop/','file_type','png')

在这里插入图片描述

例子6

坐标轴对调

load example_data;

clear g
%Compute number of models outside of gramm so that the output can be used
%as label
temp_table=rowfun(@numel,cars_table,'OutputVariableNames','N','GroupingVariables',{
    
    'Origin_Region','Model_Year'},'InputVariables','MPG');

x = temp_table.Model_Year(1:13);
y = temp_table.N(1:13);
l = temp_table.N(1:13);

g=gramm('x',x,'y',y);
g.geom_bar('LineWidth',0.5);
g.geom_label('VerticalAlignment','bottom','HorizontalAlignment','center');
g.set_names('x','Year','y','Number of models');
figure('Position',[100 100 400 300]);
g.axe_property('XTickLabelRotation',60,'XTick',x,'XTickLabel',y,'Box',1);

g.set_color_options('map','brewer_paired');
g.coord_flip();
g.draw();
g.export('file_name','my_figure','export_path','C:/Users/Administrator/Desktop/','file_type','png')

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wokaowokaowokao12345/article/details/113470008