Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a barseries plot using both grouped and stacked styles in MATLAB?

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.

like image 508
Grega Kešpret Avatar asked May 16 '11 01:05

Grega Kešpret


People also ask

What are stacked bar plots?

What is a stacked bar chart? A stacked bar chart is a type of bar graph that represents the proportional contribution of individual data points in comparison to a total. The height or length of each bar represents how much each group contributes to the total.

How do you stack plots in Seaborn?

A stacked Bar plot is a kind of bar graph in which each bar is visually divided into sub bars to represent multiple column data at once. To plot the Stacked Bar plot we need to specify stacked=True in the plot method. We can also pass the list of colors as we needed to color each sub bar in a bar.

What is a bar graph used for?

The purpose of a bar graph is to convey relational information quickly in a visual manner. The bars display the value for a particular category of data. The vertical axis on the left or right side of the bar graph is called the y-axis.

What is bar function in MATLAB?

The bar function distributes bars along the x-axis. Elements in the same row of a matrix are grouped together. For example, if a matrix has five rows and three columns, then bar displays five groups of three bars along the x-axis. The first cluster of bars represents the elements in the first row of Y.


2 Answers

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

like image 199
gnovice Avatar answered Sep 28 '22 06:09

gnovice


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

like image 27
Rasman Avatar answered Sep 28 '22 07:09

Rasman