✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,
代码获取、论文复现及科研仿真合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab完整代码及仿真定制内容点击👇
🔥 内容介绍
分组或堆叠条形图
分组或堆叠条形图是将数据按类别分组并以条形图的形式显示的一种图表。它可以帮助我们比较不同类别的数据,并了解它们之间的关系。分组或堆叠条形图通常用于显示分类数据,例如性别、年龄、收入等。
分组或堆叠条形图有两种常见的类型:分组条形图和堆叠条形图。分组条形图将不同类别的条形图并排放置,而堆叠条形图将不同类别的条形图叠加在一起。分组条形图更适合于比较不同类别的数据,而堆叠条形图更适合于显示不同类别的数据在总数据中的占比。
分组或堆叠条形图的优点在于它可以直观地显示数据,并且易于理解。它还可以帮助我们发现数据中的趋势和规律。但是,分组或堆叠条形图也有其局限性。例如,它只能显示有限数量的数据,并且当数据量较大时,分组或堆叠条形图可能会变得难以阅读。
为了使分组或堆叠条形图更加有效,我们可以注意以下几点:
-
选择合适的类别:分组或堆叠条形图的类别应该是有意义的,并且应该与研究问题相关。
-
使用合适的颜色:分组或堆叠条形图的颜色应该易于区分,并且应该与类别的含义相匹配。
-
添加标签和注释:分组或堆叠条形图应该有清晰的标签和注释,以便读者能够理解图表中的数据。
-
避免使用过多的数据:分组或堆叠条形图中的数据量应该适中,以便读者能够轻松地理解图表中的信息。
分组或堆叠条形图是一种常用的图表类型,它可以帮助我们比较不同类别的数据,并了解它们之间的关系。通过注意上述几点,我们可以使分组或堆叠条形图更加有效地传达信息。
分组或堆叠条形图的应用
分组或堆叠条形图广泛应用于各个领域,例如:
-
市场营销:分组或堆叠条形图可以用来比较不同产品或服务的销售额,或者比较不同地区或市场的销售额。
-
金融:分组或堆叠条形图可以用来比较不同股票或基金的收益率,或者比较不同国家的经济增长率。
-
教育:分组或堆叠条形图可以用来比较不同学校或班级的考试成绩,或者比较不同学生的学习成绩。
-
医疗保健:分组或堆叠条形图可以用来比较不同疾病的发病率或死亡率,或者比较不同治疗方法的有效性。
分组或堆叠条形图是一种简单而有效的图表类型,它可以帮助我们比较不同类别的数据,并了解它们之间的关系。通过注意上述几点,我们可以使分组或堆叠条形图更加有效地传达信息。
📣 完整代码
% daboxplot_demo a few examples of daboxplot functionality
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
rng('default')
% data in a cell array
data1{1} = randn([60,4]); % Humans
data1{2} = randn([60,4]); % Dogs
data1{3} = randn([60,4]); % God
data1{4} = randn([60,4]); % Potato
% data in a numreic array (+ grouping indices)
data2 = [randn([30,4]); randn([30,4]);...
randn([30,4]); randn([30,4])];
group_inx = [ones(1,30), 2.*ones(1,30), 3.*ones(1,30), 4.*ones(1,30)];
% skewed data in a numeric array (+ group indices)
data3 = [pearsrnd(0,1,-1,5,25,1); pearsrnd(0,1,-2,7,25,1); ...
pearsrnd(0,1,1,8,25,1)];
group_inx2 = [ones(1,25), 2.*ones(1,25), 3.*ones(1,25)];
% data with group differences in a cell array
data4{1} = randn([60,3]) + (0:0.5:1); % Humans
data4{2} = randn([60,3]) + (2:2:6); % Dogs
group_names = {'Humans', 'Dogs' , 'God', 'Potato'};
condition_names = {'Water', 'Land', 'Moon', 'Hyperspace'};
% an alternative color scheme for some plots
c = [0.45, 0.80, 0.69;...
0.98, 0.40, 0.35;...
0.55, 0.60, 0.79;...
0.90, 0.70, 0.30];
figure('Name', 'daboxplot_demo','WindowStyle','docked');
% default boxplots for one group and three conditions
subplot(3,3,1)
h = dabarplot(data2(:,1:3),'groups',group_inx(1:30));
% non-filled boxplots and cutomized medians
subplot(3,3,2)
h = daboxplot(data2(:,1:3),'groups',group_inx(1:60),'outsymbol','kx',...
'xtlabels', condition_names,'fill',0,'legend',group_names(1:2));
ylabel('Performance');
xl = xlim; xlim([xl(1), xl(2)+1]); % make more space for the legend
set(h.md,'Color','k','LineWidth',1.5); % customize median lines
% filled boxplots, different color scheme, non-jittered scatter underneath
subplot(3,3,3)
h = daboxplot(data2(:,1:3),'groups',group_inx(1:90),'outsymbol','k+',...
'xtlabels', condition_names,'legend',group_names(1:3),'color',c,...
'whiskers',0,'scatter',2,'jitter',0,'scattersize',13);
ylabel('Performance');
xl = xlim; xlim([xl(1), xl(2)+1]); % make more space for the legend
% transparent boxplots with no whiskers and jittered datapoints underneath
subplot(3,2,3)
h = daboxplot(data1,'scatter',2,'whiskers',0,'boxalpha',0.7,...
'xtlabels', condition_names);
ylabel('Performance');
xl = xlim; xlim([xl(1), xl(2)+0.75]); % make space for the legend
legend([h.bx(1,:)],group_names); % add the legend manually
set(gca,'FontSize',9);
% different color scheme, a color flip, different outlier symbol
subplot(3,2,4)
h = daboxplot(data2,'groups',group_inx,'xtlabels', condition_names,...
'colors',c,'fill',0,'whiskers',0,'scatter',2,'outsymbol','k*',...
'outliers',1,'scattersize',16,'flipcolors',1,'boxspacing',1.2,...
'legend',group_names);
ylabel('Performance');
xl = xlim; xlim([xl(1), xl(2)+0.75]); % make more space for the legend
set(gca,'FontSize',9);
% different color scheme, data scattered on top
subplot(3,2,5:6)
h = daboxplot(data2,'groups',group_inx,...
'xtlabels', condition_names,'colors',c,'whiskers',0,...
'scatter',1,'scattersize',15,'scatteralpha',0.5,...
'boxspacing',0.8,'legend',group_names);
ylabel('Performance');
set(gca,'FontSize',9.5);
xl = xlim; xlim([xl(1), xl(2)+0.2]); % make more space for the legend
%--------------------------------------------------------------------------
figure('Name', 'daboxplot_demo2','WindowStyle','docked');
% three groups, one condition, indicating means with dotted lines
subplot(2,3,1)
h = daboxplot(data3,'groups',group_inx2,'mean',1,'color',c,...
'xtlabels',group_names);
ylabel('Performance');
set(gca,'FontSize',12)
% using linkline to emphasize interaction effects (group*condition)
subplot(2,3,2)
h = daboxplot(data4,'linkline',1,...
'xtlabels', condition_names,'legend',group_names(1:3),...
'whiskers',0,'outliers',1,'outsymbol','r*','scatter',2,'boxalpha',0.6);
ylabel('Performance'); ylim([-2.5 8.8]);
xl = xlim; xlim([xl(1), xl(2)]); % make more space for the legend
set(gca,'FontSize',12)
% using withinline to emphasize within group differences between conditions
subplot(2,3,3)
h = daboxplot(data4{1}(:,1:2),'xtlabels', condition_names(1:2),'whiskers',0,...
'scatter',1,'scattersize',25,'scatteralpha',0.6,'withinlines',1,'outliers',0);
set(gca,'FontSize',12)
% TIP: to make the plots vertical use camroll(-90)
function h = daboxplot(Y,varargin)
% daboxplot draws neat boxplots for multiple groups and multiple conditions
%
% Description:
%
% Creates boxplots organized by condition and colored by group. Supports
% various options such as scatter, transparency, outliers, mean and
% group linking lines, scaling, etc, to maximize data readability. See
% daboxplot_demo. for examples of the use and functionality.
%
% Syntax:
%
% daboxplot(Y)
% daboxplot(Y,param,val,...)
% h = daboxplot(Y)
% h = daboxplot(Y,param,val,...)
%
% Input Arguments:
%
% Y - data input (matrix or cell array) containing all conditions and all
% groups. If Y is a matrix, each column has to correspond to different
% condition, while the groups need to be specified in 'groups' vector.
% If Y is a cell array, each cell has to contain data matrices for each
% group (columns being different conditions). In such case, the grouping
% is done automatically based on the cell structure.
%
% Optional Input Parameter Name/Value Pairs:
%
% NAME VALUE
%
% 'groups' A vector containing grouping variables. By default
% assumes a single group for a matrix data input.
%
% 'fill' 0 - non-filled boxplots (contrours only)
% 1 - boxplots filled with color (default)
%
% 'colors' The RGB matrix for box colors of different groups
% (each row corresponding to a different group). If
% boxplots are filled, these are the fill colors with
% the edges being black. If boxplots are not filled,
% these colors are used for edges. These colors can be
% also used for scatter data instead (see 'flipcolors')
% Default colors: default matlab colors
%
% 'whiskers' Draws whiskers to show min and max data values after
% disregarding the outliers (see outlier description)
% 0 - no whiskers
% 1 - draw whiskers (default)
%
% 'scatter' 0 - no datta scatter (deffault)
% 1 - on top of the boxplot
% 2 - underneath the boxplot
%
% 'scattersize' Size of the scatter markers. Default: 15
%
% 'scattercolors' Colors for the scattered data: {face, edge}
% Default: {'k','w'}
%
% 'flipcolors' Will flip the colors of scatter points and boxplots
% 0 - boxplots colored by group (default)
% 1 - scatter is colored by group
%
% 'scatteralpha' Transparency of scattered data (between 0 and 1)
% Default: 1 (completely non-transparent)
%
% 'jitter' 0 - do not jitter scattered data
% 1 - jitter scattered data (default)
%
% 'mean' 0 - do not mark the mean (default)
% 1 - mark the mean with a dotted line
%
% 'outliers' Highlights the outliers in the plot. The outliers
% are values below Q1-1.5*IQR and above Q3+1.5*IQR.
% 0 - do not highlight outliers
% 1 - highlight outliers (default)
%
% 'outfactor' Multiple of the interquartile range used to find
% outliers: below Q1-outfactor*IQR and above
% Q3+outfactor*IQR
% Default: 1.5
%
% 'outsymbol' Symbol and color for highlighting outliers.
% Default: 'rx' (red crosses).
%
% 'boxalpha' Boxplot transparency (between 0 and 1)
% Default: 1 (completely non-transparent)
%
% 'boxspacing' A real number to scale spacing between boxes in the
% same condition. Note that negative values result in
% partially overlapping boxes within the same condition
% Default: 1
%
% 'boxwidth' A real number to scale the width of all boxes. Note
% that this also controls the spacing between different
% conditions (while spacings in the same condition are
% controlled by 'boxspacing')
% Default: 1
%
% 'linkline' Superimposes lines linking boxplots across conditions
% for each group. Helps to see more clearly possible
% interaction effects between conditions and groups.
% 0 - no dash lines (default)
% 1 - dash lines
%
% 'withinlines' Draws a line between each pair of data points in
% paired datasets. Meant to be used only when plotting
% one group.
% 0 - no lines (default)
% 1 - lines
%
% 'xtlabels' Xtick labels (a cell of chars) for conditions. If
% there is only 1 condition and multiple groups, then
% xticks and xtlabels will automatically mark different
% groups.
% Default: conditions/groups are numbered in the input
% order
%
% 'legend' Names of groups (a cell) for creating a legend
% Default: no legend
%
%
% Output Arguments:
%
% h - a structure containing handles for further customization of
% the produced plot:
% cpos - condition positions
% gpos - group positions
%
% graphics objects:
% bx - boxplot box
% md - median line
% mn - mean line
% sc - scattered data markers
% ot - outlier markers
% wh - whiskers
% ln - line linking boxplots
% lg - legend
%
%
% For examples have a look at daboxplot_demo.m
% Also see: daviolinplot.m and dabarplot.m
%
%
% Povilas Karvelis
% 15/04/2019
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
h = struct;
p = inputParser;
% specify default options
addOptional(p, 'groups', []);
addOptional(p, 'fill', 1);
addOptional(p, 'colors', get(gca,'colororder'));
addOptional(p, 'whiskers', 1);
addOptional(p, 'scatter', 0);
addOptional(p, 'scattersize', 15)
addOptional(p, 'scattercolors', {'k','w'});
addOptional(p, 'flipcolors', 0);
addOptional(p, 'scatteralpha', 1);
addOptional(p, 'jitter', 1);
addOptional(p, 'mean', 0);
addOptional(p, 'outliers', 1);
addOptional(p, 'outfactor', 1.5);
addOptional(p, 'outsymbol', 'rx');
addOptional(p, 'boxalpha', 1);
addOptional(p, 'boxspacing', 1);
addOptional(p, 'boxwidth', 1);
addOptional(p, 'linkline',0);
addOptional(p, 'withinlines',0);
addOptional(p, 'xtlabels', []);
addOptional(p, 'legend', []);
% parse the input options
parse(p, varargin{:});
confs = p.Results;
% get group indices and labels
if ~isempty(confs.groups)
[Gi,Gn,Gv] = grp2idx(confs.groups);
num_groups = numel(Gv);
end
% find the number of groups
if iscell(Y)
num_groups = numel(Y);
y = []; Gi = [];
for g = 1:num_groups
y = [y; Y{g}];
Gi = [Gi; g*ones(size(Y{g},1),1)];
end
% default numbered group labels
if ~exist('Gn','var')
for g = 1:num_groups
Gn{g} = num2str(g);
end
end
Y = y; % replace the cell with a data array
elseif ismatrix(Y)
% assume 1 group if none are specified
if isempty(confs.groups)
Gi = ones(size(Y,1),1);
num_groups = 1;
end
end
% find condition positions
if any(size(Y)==1)
Y = Y(:);
cpos = 1;
else
cpos = 1:size(Y,2);
end
num_locs = numel(cpos);
% use condition positions to scale spacings
gpos=[];
if num_locs==1
gpos = (1:num_groups)';
box_width = 1/3*confs.boxwidth;
cpos=gpos;
else
if num_groups==1
gpos = cpos;
box_width = 1/3*confs.boxwidth;
else
box_width = (2/3)/(num_groups+1)*confs.boxwidth; % calculate box width
loc_sp = (box_width/3)*confs.boxspacing; % local spacing between boxplots
% set group positions for each group
for g = 1:num_groups
gpos = [gpos; cpos + (g-(num_groups+1)/2)*(box_width + loc_sp)];
end
end
end
h.gpos = gpos;
h.cpos = cpos;
% loop over groups
for g = 1:num_groups
% get percentiles
pt = prctile(Y(Gi==g,:),[2 9 25 50 75 91 98]);
means = mean(Y(Gi==g,:));
if size(pt,1)==1 pt=pt'; end % for plotting one condition
IQR = (pt(5,:)-pt(3,:));
% create coordinates for drawing boxes
y25 = reshape([pt(3,:); pt(3,:)], 1, []);
y75 = reshape([pt(5,:); pt(5,:)], 1, []);
x1 = [gpos(g,:) - box_width/2; gpos(g,:) - box_width/2];
x2 = [gpos(g,:) + box_width/2; gpos(g,:) + box_width/2];
box_ycor = [y75; y25];
box_xcor = reshape([x1; x2],2,[]);
box_mdcor = reshape([pt(4,:); pt(4,:)], 1, []);
box_mncor = reshape([means; means], 1, []);
% create coordinates for drawing whiskers with cross-hatches and ends
hat_xcor = [gpos(g,:) - box_width/4; gpos(g,:) + box_width/4];
whi_xcor = [gpos(g,:); gpos(g,:)];
% draw one box at a time
for k = 1:num_locs
data_vals = Y(Gi==g,k); % data for a single box
% determine outliers and whisker length
ol = data_vals<(pt(3,k)-confs.outfactor*IQR(k)); % indices of lower outliers
ou = data_vals>(pt(5,k)+confs.outfactor*IQR(k)); % indices of upper outliers
whi_ycor(:,1,k) = [min(data_vals(~ol)), pt(3,k)]; % lower whisker
whi_ycor(:,2,k) = [max(data_vals(~ou)), pt(5,k)]; % upper whisker
% jitter or not
if confs.jitter==1
xdata = gpos(g,k).*ones(numel(Y(Gi==g,k)),1) + ...
(box_width/3).*(0.5 - rand(numel(Y(Gi==g,k)),1));
elseif confs.jitter==0
xdata = gpos(g,k).*ones(numel(Y(Gi==g,k)),1);
end
% store data in case it's needed for withinlines
scdata(:,:,k,g) = [xdata, data_vals];
% index values for each box
wk = (1:2)+2*(k-1);
Xx = box_xcor(1:2,wk);
Yy = box_ycor(1:2,wk);
% filled or not filled boxes
if confs.fill==0
% no fill box
h.bx(k,g) = line([Xx(:,1)' Xx(1,:) Xx(:,2)' Xx(2,:)],...
[Yy(:,1)' Yy(1,:) Yy(:,2)' Yy(2,:)],...
'color',confs.colors(g,:),'LineWidth',1.5);
hold on;
% draw the median
h.md(k,g) = line(Xx(1,:), box_mdcor(wk),...
'color',confs.colors(g,:), 'LineWidth', 2);
% draw the mean
if confs.mean==1
h.mn(k,g) = line(Xx(1,:),box_mncor(wk),'LineStyle',':',...
'color',confs.colors(g,:),'LineWidth', 1.5);
end
elseif confs.fill==1
% box filled with color
h.bx(k,g) = fill([Xx(:,1)' Xx(1,:) Xx(:,2)' Xx(2,[2,1])],...
[Yy(:,1)' Yy(1,:) Yy(:,2)' Yy(2,:)],confs.colors(g,:));
set(h.bx(k,g),'FaceAlpha',confs.boxalpha);
hold on;
% draw the median
h.md(k,g) = line(Xx(1,:), box_mdcor(wk),...
'color','k', 'LineWidth', 2);
% draw the mean
if confs.mean==1
h.mn(k,g) = line(Xx(1,:),box_mncor(wk),'LineStyle',':',...
'color','k','LineWidth', 1.5);
end
end
ox = data_vals>max(data_vals); % default - no outliers
% draw outliers
if confs.outliers==1
ox = data_vals<whi_ycor(1,1,k) | data_vals>whi_ycor(1,2,k);
h.ot(k,g) = scatter(xdata(ox),data_vals(ox),confs.scattersize,...
confs.outsymbol);
end
% draw whiskers
if confs.whiskers==1
h.wh(k,g,:) = plot(whi_xcor(:,k),whi_ycor(:,1,k),'k-',...
hat_xcor(:,k),[whi_ycor(1,1,k) whi_ycor(1,1,k)],'k-',...
whi_xcor(:,k),whi_ycor(:,2,k),'k-',...
hat_xcor(:,k),[whi_ycor(1,2,k) whi_ycor(1,2,k)],'k-',...
'LineWidth',1);
end
% scatter on top of the boxplots
if confs.scatter==1 || confs.scatter==2
h.sc(k,g) = scatter(xdata(~ox),data_vals(~ox),...
confs.scattersize,...
'MarkerFaceColor', confs.scattercolors{1},...
'MarkerEdgeColor', confs.scattercolors{2},...
'MarkerFaceAlpha', confs.scatteralpha);
hold on;
end
end
% link the medians of the boxplots with a line
if confs.linkline==1
h.ln(g) = line(gpos(g,:),pt(4,:),'color',confs.colors(g,:),...
'LineStyle','-.','LineWidth',1.5);
end
% link individual within group data points
if confs.withinlines==1
for s = 1:size(scdata,1)
h.wl(g) = plot(squeeze(scdata(s,1,:,g)),...
squeeze(scdata(s,2,:,g)),'color', [0.8 0.8 0.8]);
uistack(h.wl(g),'bottom')
end
end
% put scattered data underneath boxplots
if confs.scatter==1
if confs.mean==1
uistack(h.mn(:,g),'bottom')
end
uistack(h.md(:,g),'bottom')
uistack(h.bx(:,g),'bottom')
if confs.whiskers==1
uistack(h.wh(:,g,:),'bottom')
end
elseif confs.scatter==2
uistack(h.sc(:,g),'bottom')
end
end
% move lines to the background
if confs.linkline==1
uistack(h.ln,'bottom')
end
% flip scatter and box colors and make a legend
if confs.flipcolors==1
box_class = class(h.bx); % box filled or no
if strcmp(box_class,'matlab.graphics.primitive.Patch')
set(h.bx,'FaceColor',confs.scattercolors{1});
set(h.md,'Color',confs.scattercolors{2});
if confs.mean==1
set(h.mn,'Color',confs.scattercolors{2});
end
else
set(h.bx,'Color',confs.scattercolors{1});
set(h.md,'Color',confs.scattercolors{1});
if confs.mean==1
set(h.mn,'Color',confs.scattercolors{1});
end
end
for g = 1:num_groups
set(h.sc(:,g),'MarkerFaceColor',confs.colors(g,:))
end
% add a legend based on scatter colors
if ~isempty(confs.legend)
h.lg = legend(h.sc(1,:),confs.legend);
end
else
% add a legend based on box colors
if ~isempty(confs.legend)
h.lg = legend(h.bx(1,:),confs.legend);
end
end
% set ticks and labels
set(gca,'XTick',cpos,'XTickLabels',cpos,'box','off');
if ~isempty(confs.xtlabels)
set(gca,'XTickLabels',confs.xtlabels,'XTick',cpos);
end
xlim([gpos(1)-3*box_width, gpos(end)+3*box_width]); % adjust x-axis margins
end
⛳️ 运行结果
🔗 参考文献
🎈 部分理论引用网络文献,若有侵权联系博主删除
🎁 关注我领取海量matlab电子书和数学建模资料
👇 私信完整代码、论文复现、期刊合作、论文辅导及科研仿真定制
1 各类智能优化算法改进及应用
生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化
2 机器学习和深度学习方面
卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断
2.图像处理方面
图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知
3 路径规划方面
旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化
4 无人机应用方面
无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配、无人机安全通信轨迹在线优化
5 无线传感器定位及布局方面
传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化
6 信号处理方面
信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化
7 电力系统方面
微电网优化、无功优化、配电网重构、储能配置
8 元胞自动机方面
交通流 人群疏散 病毒扩散 晶体生长
9 雷达方面
卡尔曼滤波跟踪、航迹关联、航迹融合