matlab 实现 | 堆叠直方图

画堆叠直方图的打包函数

function [] = plotBarStackGroups(stackData, groupLabels)
%% Plot a set of stacked bars, but group them according to labels provided.
%%
%% Params: 
%%      stackData is a 3D matrix (i.e., stackData(i, j, k) => (Group, Stack, StackElement)) 
%%      groupLabels is a CELL type (i.e., { 'a', 1 , 20, 'because' };)
%%
%% Copyright 2011 Evan Bollig (bollig at scs DOT fsu ANOTHERDOT edu
%%
%% 
NumGroupsPerAxis = size(stackData, 1);
NumStacksPerGroup = size(stackData, 2);


% Count off the number of bins
groupBins = 1:NumGroupsPerAxis;
MaxGroupWidth = 0.65; % Fraction of 1. If 1, then we have all bars in groups touching
groupOffset = MaxGroupWidth/NumStacksPerGroup;
figure
    hold on; 
for i=1:NumStacksPerGroup

    Y = squeeze(stackData(:,i,:));
    
    % Center the bars:
    
    internalPosCount = i - ((NumStacksPerGroup+1) / 2);
    
    % Offset the group draw positions:
    groupDrawPos = (internalPosCount)* groupOffset + groupBins;
    
    h(i,:) = bar(Y, 'stacked');
    set(h(i,:),'BarWidth',groupOffset);
    set(h(i,:),'XData',groupDrawPos);
end
hold off;
set(gca,'XTickMode','manual');
set(gca,'XTick',1:NumGroupsPerAxis);
set(gca,'XTickLabelMode','manual');
set(gca,'XTickLabel',groupLabels);
end 

调用函数

   A =[

    3462        2077         692         692
    409         245          82          82
    2417        1450         483         483
    8554        5132        1711        1711
    1620         972         324         324
    54          32          11          11
    5000        3000        1000        1000
    609         365         122         122
    693         416         139         139];

    A=A(:,2:4);
    A=reshape(A,[9 1 3]);
    x={'Center','Donut','Edge-local','Edge-ring','Local','Near-full','None','Random','Scratch'} 
    plotBarStackGroups(A,x);

结果如图

MATLAB中,可以使用函数bar3来绘制三维直方图。与二维直方图相似,只需将bar替换为bar3即可。以下是一些常用的用法: 1. 绘制长度为y的三维直方图(宽度系统自动匹配): bar3(y); 2. 绘制长度为y,宽度为width的三维直方图: bar3(y, width); 3. 在x处生成长度为y的三维直方图: bar3(x, y); 4. 在x处生成长度为y,宽度为width的三维直方图: bar3(x, y, width); 5. 生成两组高度分别为a b c和d e f,宽度都为0.5的三维直方图: y = \[a, b, c; d, e, f\]; bar3(y, 0.5); 6. 将两组三维直方图绘制在一起: y = \[a, b, c; d, e, f\]; bar3(y, 'grouped'); 7. 将两组三维直方图堆叠在一起: y = \[a, b, c; d, e, f\]; bar3(y, 'stack'); 8. 将三维直方图横着放置: bar3h(y); 此外,如果需要绘制带渐变色的三维彩色柱状图,可以使用自定义函数scatterbar。该函数可以根据用户指定的散点数据绘制柱状图,并支持渐变色。\[2\] 如果需要将三维直方图放置在多重子图中,可以使用subplot函数来实现。例如,可以使用subplot(2,2,1)将三维直方图放置在2x2的子图中的第一个位置。\[3\] #### 引用[.reference_title] - *1* *3* [Matlab绘制三维直方图、饼图](https://blog.csdn.net/zhangkaikai36/article/details/123435891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [MATLAB小技巧之十:利用MATLAB绘制三维彩色柱状图[转载]](https://blog.csdn.net/weixin_36462703/article/details/115809480)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值