✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,
代码获取、论文复现及科研仿真合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab完整代码及仿真定制内容点击👇
🔥 内容介绍
分组或堆叠条形图
分组或堆叠条形图是将数据按类别分组并以条形图的形式显示的一种图表。它可以帮助我们比较不同类别的数据,并了解它们之间的关系。分组或堆叠条形图通常用于显示分类数据,例如性别、年龄、收入等。
分组或堆叠条形图有两种常见的类型:分组条形图和堆叠条形图。分组条形图将不同类别的条形图并排放置,而堆叠条形图将不同类别的条形图叠加在一起。分组条形图更适合于比较不同类别的数据,而堆叠条形图更适合于显示不同类别的数据在总数据中的占比。
分组或堆叠条形图的优点在于它可以直观地显示数据,并且易于理解。它还可以帮助我们发现数据中的趋势和规律。但是,分组或堆叠条形图也有其局限性。例如,它只能显示有限数量的数据,并且当数据量较大时,分组或堆叠条形图可能会变得难以阅读。
为了使分组或堆叠条形图更加有效,我们可以注意以下几点:
-
选择合适的类别:分组或堆叠条形图的类别应该是有意义的,并且应该与研究问题相关。
-
使用合适的颜色:分组或堆叠条形图的颜色应该易于区分,并且应该与类别的含义相匹配。
-
添加标签和注释:分组或堆叠条形图应该有清晰的标签和注释,以便读者能够理解图表中的数据。
-
避免使用过多的数据:分组或堆叠条形图中的数据量应该适中,以便读者能够轻松地理解图表中的信息。
分组或堆叠条形图是一种常用的图表类型,它可以帮助我们比较不同类别的数据,并了解它们之间的关系。通过注意上述几点,我们可以使分组或堆叠条形图更加有效地传达信息。
分组或堆叠条形图的应用
分组或堆叠条形图广泛应用于各个领域,例如:
-
市场营销:分组或堆叠条形图可以用来比较不同产品或服务的销售额,或者比较不同地区或市场的销售额。
-
金融:分组或堆叠条形图可以用来比较不同股票或基金的收益率,或者比较不同国家的经济增长率。
-
教育:分组或堆叠条形图可以用来比较不同学校或班级的考试成绩,或者比较不同学生的学习成绩。
-
医疗保健:分组或堆叠条形图可以用来比较不同疾病的发病率或死亡率,或者比较不同治疗方法的有效性。
分组或堆叠条形图是一种简单而有效的图表类型,它可以帮助我们比较不同类别的数据,并了解它们之间的关系。通过注意上述几点,我们可以使分组或堆叠条形图更加有效地传达信息。
📣 完整代码
% daboxplot_demo a few examples of daboxplot functionality%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clear allrng('default')% data in a cell arraydata1{1} = randn([60,4]); % Humansdata1{2} = randn([60,4]); % Dogsdata1{3} = randn([60,4]); % Goddata1{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 arraydata4{1} = randn([60,3]) + (0:0.5:1); % Humansdata4{2} = randn([60,3]) + (2:2:6); % Dogsgroup_names = {'Humans', 'Dogs' , 'God', 'Potato'};condition_names = {'Water', 'Land', 'Moon', 'Hyperspace'};% an alternative color scheme for some plotsc = [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 conditionssubplot(3,3,1)h = dabarplot(data2(:,1:3),'groups',group_inx(1:30));% non-filled boxplots and cutomized medianssubplot(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 legendset(h.md,'Color','k','LineWidth',1.5); % customize median lines% filled boxplots, different color scheme, non-jittered scatter underneathsubplot(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 underneathsubplot(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 legendlegend([h.bx(1,:)],group_names); % add the legend manuallyset(gca,'FontSize',9);% different color scheme, a color flip, different outlier symbolsubplot(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 legendset(gca,'FontSize',9);% different color scheme, data scattered on topsubplot(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 linessubplot(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 legendset(gca,'FontSize',12)% using withinline to emphasize within group differences between conditionssubplot(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 optionsaddOptional(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 optionsparse(p, varargin{:});confs = p.Results;% get group indices and labelsif ~isempty(confs.groups)[Gi,Gn,Gv] = grp2idx(confs.groups);num_groups = numel(Gv);end% find the number of groupsif iscell(Y)num_groups = numel(Y);y = []; Gi = [];for g = 1:num_groupsy = [y; Y{g}];Gi = [Gi; g*ones(size(Y{g},1),1)];end% default numbered group labelsif ~exist('Gn','var')for g = 1:num_groupsGn{g} = num2str(g);endendY = y; % replace the cell with a data arrayelseif ismatrix(Y)% assume 1 group if none are specifiedif isempty(confs.groups)Gi = ones(size(Y,1),1);num_groups = 1;endend% find condition positionsif any(size(Y)==1)Y = Y(:);cpos = 1;elsecpos = 1:size(Y,2);endnum_locs = numel(cpos);% use condition positions to scale spacingsgpos=[];if num_locs==1gpos = (1:num_groups)';box_width = 1/3*confs.boxwidth;cpos=gpos;elseif num_groups==1gpos = cpos;box_width = 1/3*confs.boxwidth;elsebox_width = (2/3)/(num_groups+1)*confs.boxwidth; % calculate box widthloc_sp = (box_width/3)*confs.boxspacing; % local spacing between boxplots% set group positions for each groupfor g = 1:num_groupsgpos = [gpos; cpos + (g-(num_groups+1)/2)*(box_width + loc_sp)];endendendh.gpos = gpos;h.cpos = cpos;% loop over groupsfor g = 1:num_groups% get percentilespt = 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 conditionIQR = (pt(5,:)-pt(3,:));% create coordinates for drawing boxesy25 = 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 endshat_xcor = [gpos(g,:) - box_width/4; gpos(g,:) + box_width/4];whi_xcor = [gpos(g,:); gpos(g,:)];% draw one box at a timefor k = 1:num_locsdata_vals = Y(Gi==g,k); % data for a single box% determine outliers and whisker lengthol = data_vals<(pt(3,k)-confs.outfactor*IQR(k)); % indices of lower outliersou = data_vals>(pt(5,k)+confs.outfactor*IQR(k)); % indices of upper outlierswhi_ycor(:,1,k) = [min(data_vals(~ol)), pt(3,k)]; % lower whiskerwhi_ycor(:,2,k) = [max(data_vals(~ou)), pt(5,k)]; % upper whisker% jitter or notif confs.jitter==1xdata = 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==0xdata = gpos(g,k).*ones(numel(Y(Gi==g,k)),1);end% store data in case it's needed for withinlinesscdata(:,:,k,g) = [xdata, data_vals];% index values for each boxwk = (1:2)+2*(k-1);Xx = box_xcor(1:2,wk);Yy = box_ycor(1:2,wk);% filled or not filled boxesif confs.fill==0% no fill boxh.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 medianh.md(k,g) = line(Xx(1,:), box_mdcor(wk),...'color',confs.colors(g,:), 'LineWidth', 2);% draw the meanif confs.mean==1h.mn(k,g) = line(Xx(1,:),box_mncor(wk),'LineStyle',':',...'color',confs.colors(g,:),'LineWidth', 1.5);endelseif confs.fill==1% box filled with colorh.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 medianh.md(k,g) = line(Xx(1,:), box_mdcor(wk),...'color','k', 'LineWidth', 2);% draw the meanif confs.mean==1h.mn(k,g) = line(Xx(1,:),box_mncor(wk),'LineStyle',':',...'color','k','LineWidth', 1.5);endendox = data_vals>max(data_vals); % default - no outliers% draw outliersif confs.outliers==1ox = 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 whiskersif confs.whiskers==1h.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 boxplotsif confs.scatter==1 || confs.scatter==2h.sc(k,g) = scatter(xdata(~ox),data_vals(~ox),...confs.scattersize,...'MarkerFaceColor', confs.scattercolors{1},...'MarkerEdgeColor', confs.scattercolors{2},...'MarkerFaceAlpha', confs.scatteralpha);hold on;endend% link the medians of the boxplots with a lineif confs.linkline==1h.ln(g) = line(gpos(g,:),pt(4,:),'color',confs.colors(g,:),...'LineStyle','-.','LineWidth',1.5);end% link individual within group data pointsif confs.withinlines==1for 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')endend% put scattered data underneath boxplotsif confs.scatter==1if confs.mean==1uistack(h.mn(:,g),'bottom')enduistack(h.md(:,g),'bottom')uistack(h.bx(:,g),'bottom')if confs.whiskers==1uistack(h.wh(:,g,:),'bottom')endelseif confs.scatter==2uistack(h.sc(:,g),'bottom')endend% move lines to the backgroundif confs.linkline==1uistack(h.ln,'bottom')end% flip scatter and box colors and make a legendif confs.flipcolors==1box_class = class(h.bx); % box filled or noif strcmp(box_class,'matlab.graphics.primitive.Patch')set(h.bx,'FaceColor',confs.scattercolors{1});set(h.md,'Color',confs.scattercolors{2});if confs.mean==1set(h.mn,'Color',confs.scattercolors{2});endelseset(h.bx,'Color',confs.scattercolors{1});set(h.md,'Color',confs.scattercolors{1});if confs.mean==1set(h.mn,'Color',confs.scattercolors{1});endendfor g = 1:num_groupsset(h.sc(:,g),'MarkerFaceColor',confs.colors(g,:))end% add a legend based on scatter colorsif ~isempty(confs.legend)h.lg = legend(h.sc(1,:),confs.legend);endelse% add a legend based on box colorsif ~isempty(confs.legend)h.lg = legend(h.bx(1,:),confs.legend);endend% set ticks and labelsset(gca,'XTick',cpos,'XTickLabels',cpos,'box','off');if ~isempty(confs.xtlabels)set(gca,'XTickLabels',confs.xtlabels,'XTick',cpos);endxlim([gpos(1)-3*box_width, gpos(end)+3*box_width]); % adjust x-axis marginsend
⛳️ 运行结果


🔗 参考文献
🎈 部分理论引用网络文献,若有侵权联系博主删除
🎁 关注我领取海量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 雷达方面
卡尔曼滤波跟踪、航迹关联、航迹融合
本文围绕Matlab展开,介绍了分组或堆叠条形图,包括类型、优缺点及使用注意事项,还阐述其在多领域的应用。同时列举了Matlab在智能优化算法、机器学习、图像处理、路径规划等多个科研领域的应用场景及相关问题。
1156

被折叠的 条评论
为什么被折叠?



