Grouped bar chart with single data point per group

14 views (last 30 days)
Ed
Ed on 1 Oct 2013
Commented: Ed on 1 Oct 2013
Hi,
I have a script with takes a data structure as an input, and depending on various properties set by the user produces graphs as appropriate.
In some cases data is plotted as a grouped bar chart, so we have multiple 'series' (in excel-speak).
Problem is that when we go to a single x value (one row of data), matlab decides to plot this as if there were one 'series' with n x-values, apposed to n-series with one x-value (which is what I want).
For example
Y = round(rand(5,3)*10);
figure;
subplot(2,2,1); bar(Y,'grouped'); title('Group')
Gives the following
Whereas
Y = round(rand(1,3)*10);
figure;
subplot(2,2,1); bar(Y,'grouped')
title('Group')
Gives this
(The data values are a little different because of the rand)
Not something like this
Any thoughts?
I've played around with transposing the matrices/ using staked bars etc. but this doesn't do much good. (In fact, grouped/stacked come out identical for this case, this is not, by my interpretation, of how bar is documented in matlab) Ideally I want to avoid adding null data to the bar chart as this screws things up later on.
Many thanks, Ed

Answers (1)

Sean de Wolski
Sean de Wolski on 1 Oct 2013
Edited: Sean de Wolski on 1 Oct 2013
Hi Ed,
The trick here is to nan-pad Y to trick bar into thinking there are multiple groups:
Y = round(rand(1,3)*10);
figure;
if isrow(Y);
Y = vertcat(Y,nan(size(Y)));
end
bar(Y,'grouped')
xlim([0.5 1.5]) %know this to be true if no x
title('Group')
  1 Comment
Ed
Ed on 1 Oct 2013
Hi Sean,
Thanks for the answer, unfortunately I wanted to avoid using adding NaN data, as it would require further adjustments later on (ie. when I tabulate the graphed data, and line it up with the axes beneath the groups).
But if this is the only way, fair enough. It shoulnd't be too hard to add some extra work-arounds to the tables.
Thanks again, Ed

Sign in to comment.

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!