如何在 MATLAB 中使用分组和堆叠样式创建条形图?

发布于 2024-11-07 13:48:57 字数 806 浏览 4 评论 0原文

MATLAB bar 文档 声明如下:

bar(...,'style') 指定条形的样式。 “样式”是“分组”“堆叠”。默认显示模式是“分组”。

但是,我想同时实现两者。让我通过举个例子来详细说明:

Y = [1.0 0.5 0.7
     2.0 1.5 2.0
     5.0 4.0 5.0
     4.0 4.0 4.5
     3.0 2.0 2.0];

bar(Y,'group');

此代码生成以下分组条形图,其中 5 组不同的 3 个条形图分组在一起:

在此处输入图像描述

bar([repmat(0.5,5,1) Y(:,1)-0.5],'stack');

此代码仅使用上面定义的矩阵 Y 的第一列生成以下堆叠条形图:

在此处输入图像描述

我想合并这两个,以获得同时分组和堆叠的条形图。因此,所需的结果将类似于第一张图片,并且一组中的三个条中的每一个都会像第二张图片一样堆叠。

The MATLAB bar documentation states the following:

bar(...,'style') specifies the style of the bars. 'style' is 'grouped' or 'stacked'. Default mode of display is 'grouped'.

However, I would like to achieve both at the same time. Let me elaborate by giving an example:

Y = [1.0 0.5 0.7
     2.0 1.5 2.0
     5.0 4.0 5.0
     4.0 4.0 4.5
     3.0 2.0 2.0];

bar(Y,'group');

This code produces the following grouped barseries plot, with 5 different sets of 3 bars grouped together:

enter image description here

bar([repmat(0.5,5,1) Y(:,1)-0.5],'stack');

And this code produces the following stacked barseries plot, using just the first column of the above defined matrix Y:

enter image description here

I would like to merge these two, to get a barseries plot which is grouped and stacked at the same time. So the desired result would be like the first picture and each of the three bars in a set would be stacked like the second picture.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百思不得你姐 2024-11-14 13:48:57

我不知道如何让 BAR 绘制组合分组/为您堆积的条形图。但是,您可以自己完成此操作,方法是在 Y 数据中希望分隔条形组的任意位置添加零行,然后修改 x 轴 刻度线相应地勾选标签。这是一个示例:

>> Y = [1 2 3; ...  %# Sample data
        2 3 4; ...
        3 4 5; ...
        4 5 6; ...
        5 6 7; ...
        6 7 8; ...
        7 8 9; ...
        8 9 10; ...
        9 10 11];
>> newY = reshape([reshape(Y,3,[]); zeros(1,numel(Y)/3)],[],3)  %# Add zeroes
                                                                %#   for spacing
newY =

     1     2     3
     2     3     4
     3     4     5
     0     0     0    %# <--- Note zero rows
     4     5     6
     5     6     7
     6     7     8
     0     0     0
     7     8     9
     8     9    10
     9    10    11
     0     0     0

>> bar(newY,'stacked');  %# Create a stacked histogram
>> set(gca,'XLim',[0 12],'XTick',2:4:10,'XTickLabel',1:3);  %# Modify axes

这是结果图:

在此处输入图像描述

There's no way I know of to get BAR to plot a combination grouped/stacked bar chart for you. However, you can do it yourself by adding rows of zeroes to your Y data wherever you want groups of bars to be separated, then modifying the x-axis tick marks and tick labels accordingly. Here's an example:

>> Y = [1 2 3; ...  %# Sample data
        2 3 4; ...
        3 4 5; ...
        4 5 6; ...
        5 6 7; ...
        6 7 8; ...
        7 8 9; ...
        8 9 10; ...
        9 10 11];
>> newY = reshape([reshape(Y,3,[]); zeros(1,numel(Y)/3)],[],3)  %# Add zeroes
                                                                %#   for spacing
newY =

     1     2     3
     2     3     4
     3     4     5
     0     0     0    %# <--- Note zero rows
     4     5     6
     5     6     7
     6     7     8
     0     0     0
     7     8     9
     8     9    10
     9    10    11
     0     0     0

>> bar(newY,'stacked');  %# Create a stacked histogram
>> set(gca,'XLim',[0 12],'XTick',2:4:10,'XTickLabel',1:3);  %# Modify axes

And here's the resulting figure:

enter image description here

疯了 2024-11-14 13:48:57

组与堆叠是一种非此即彼的信息类型。您需要发挥创意来完成您想做的事情:

bar(Y,'stacked','BarWidth',0.3);
hold;
X = Y.*abs(randn(5,3));
bar(X,'stacked','BarWidth',0.3, 'Xdata', 1.3:1:5.3);
Z = Y.*abs(randn(5,3));
bar(X,'stacked','BarWidth',0.3, 'Xdata', 0.7:1:4.7);

快速总结:从堆叠数据开始,确保 barwidth 足够小,以便将数据正确地放入图表中,保留您的绘图,然后添加另一个带有数据偏移量的条形图 (Xdata)。

在此处输入图像描述

group vs stacked is an either-or type of information. you need to be creative to do what you want to do:

bar(Y,'stacked','BarWidth',0.3);
hold;
X = Y.*abs(randn(5,3));
bar(X,'stacked','BarWidth',0.3, 'Xdata', 1.3:1:5.3);
Z = Y.*abs(randn(5,3));
bar(X,'stacked','BarWidth',0.3, 'Xdata', 0.7:1:4.7);

quick rundown: start off with stacked data, make sure the barwidth is small enough to fit data properly to the graph, hold your plot, then add the other bar plot with a data offset (Xdata).

enter image description here

兰花执着 2024-11-14 13:48:57

我发现这个功能非常有用并且很容易定制。我相信它为分组和堆叠条形图问题提供了另一种优雅的解决方案。

http://www.mathworks.com/matlabcentral/fileexchange /32884-plot-groups-of-stacked-bars

I found this function to be quite useful and easily customized. I believe it provides another elegant solution to the grouped-and-stacked-bar-plot problem.

http://www.mathworks.com/matlabcentral/fileexchange/32884-plot-groups-of-stacked-bars

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文