Matlab Gramm drawing toolbox

Introduction

Gramm is Matlab's data visualization toolbox, which can easily and flexibly generate publication-quality charts. Gramm does not need to draw colors or subgraphs in a loop, and can automatically generate colors and legends, handle axis limits, etc. One of Gramm's advantages is to conduct statistics and comparisons, especially comparisons. I stumbled upon this super useful drawing toolbox, just record it, and follow-up will continue to add skills. Download link: https://github.com/piermorel/gramm . Installation is also introduced on github, which is very simple.

Official example

Function: https://github.com/piermorel/gramm/blob/master/gramm%20cheat%20sheet.pdf
Example: https://htmlpreview.github.io/?https://github.com/piermorel/gramm/ blob/master/html/examples.html
Matlab forum example: https://www.mathworks.com/matlabcentral/fileexchange/54465-gramm-complete-data-visualization-toolbox-ggplot2-r-like

work process

The typical workflow of Gramm is as follows: First, provide gramm with relevant data about graphics: X and Y variables, as well as grouping variables that will determine colors, sub-graph rows/columns, etc.; then, add graphics layers to the graphics, and each instruction can Add a layer, all layers provide many customization options.

Example 1

The fuel economy of a new car changes over time. Different colors indicate the number of cylinders (that is, the number of cylinders is used for data classification).

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')

Insert picture description here

Example 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')

Insert picture description here

Example 3

Set color

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')

Insert picture description here

Example 4

Use the handle to add text attributes

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));

Insert picture description here

Example 5

Axis attribute setting, set ticklabel, and rotate

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')

Insert picture description here

Example 6

Axis swap

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')

Insert picture description here

Guess you like

Origin blog.csdn.net/wokaowokaowokao12345/article/details/113470008
Recommended