How can I make two axes on a clustered stack bar chart?

2 次查看(过去 30 天)
I have made a clustered stack bar chart using Plot Groups of Stacked Bars by Evan on file exchange: http://www.mathworks.co.uk/matlabcentral/fileexchange/32884-plot-groups-of-stacked-bars/content/plotBarStackGroups.m
My data is in a 25x3x2 matrix. I currently have labels for the clusters (the 25 categories), and have used
legend('With', 'Without');
to label the two variables within each bar.
I am missing labels for the individual bars (the 3 bars per cluster). Is there a way to have x labels for both the individual bars and the cluster, like the "A cluster-of-stacked-bars graph" shown on the webpage http://www.burningcutlery.com/derek/bargraph/?

采纳的回答

Jonathan Epperl
Jonathan Epperl 2012-11-20
Here is simple tweak to plotBarStackGroups.m that will allow you to label every single bar from left to right.
Just before the for loop, insert
xtick = [];
Just before the end of the for loop, insert
xtick = [xtick; groupDrawPos(:)];
And then, after the hold off insert
set(gca,'XTickMode','manual');
xts = sort(xtick);
set(gca,'XTickLabelMode','manual','XTick',xts,'XTickLabel',groupLabels);
end
and delete what else is there.
Now you should be able to call it as before, but now the second parameter needs to be a cell array with a string containing the label of every bar, from left to right. Nice feature though: If there are less elements in the cell array than bars, then Matlab cycles through. Example:
D=rand(25,3,2); % Make some Data
C = mat2cell(char(randi(25,[25*3 1])+65),ones(25*3,1)); % and some labels
plotBarStackGroups(D,C); % plot with a label for every bar
plotBarStackGroups(D,C(1:3)); % that happens if you supply only 3 labels
You'll note that I used single-character labels. As soon as they get bigger it'll get very messy. And unfortunately, there is no easy way to rotate TickLabels in Matlab, you'll have to resort to the FEX there.
Also, there are now no more group labels, since those are only going to be less, you can maybe fix them there using text().
A third option would be to leave plotBarStackGroups as is and instead add labels on top of each bar, again using text. If you feel you need to do that let us know here, I'll try and help you with it.
  1 个评论
Renee
Renee 2012-11-21
Thank you! It works great. For the group labels, I added a second axis instead, which almost gives what I want.
position = get(axis1,'Position');
position(2) = position(2)-0.03;
position(4) = position(2)-0.0000001;
axis4 = axes('Position',position,...
'XTickMode','manual',...
'XTick',1:NumGroupsPerAxis,...
'XTickLabelMode','manual',...
'Color','none',...
'TickLength',[0 0],...
'YTickLabel','',...
'YTick', [],...
'YColor','white',...
'XColor', 'k',...
'XTickLabel',groupLabels);
xlabel('Minutes','FontSize', 18)
axis([0 26.5 0 1]);
ax2=axes('position',position);
xlim=get(axis4,'XLim');
xtick=get(axis4,'XTick');
delt=diff(xtick);
xtick2=[-xtick(1)+0.5*[delt delt(end)]+xtick(1:end)];
xticklabels=get(axis4,'XTickLabel');
set(ax2,'Xlim',xlim,...
'XTick',xtick2,...
'TickLength',[0.01 0],...
'XTicklabel','',...
'Color','None',...
'YTickLabel','',...
'YColor','white',...
'YTick', []);
axis([0 26.5 0 1]);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by