MATLAB: XTick labels and Stacking in bar plot

colormapplot

I have two questions: 1. I am not getting the XTick labels for the green colored bar using code1. I don't know how to stop this overlapping of xtick labels.
% code1
PErr = [235.6923 5.5125];
wd = 0.2;
bar(1,PErr(1),wd,'facecolor','r');
set(gca,'XTickLabel',{'MODEL1'});
hold on;
bar(2,PErr(2),wd,'facecolor','g');
set(gca,'XTickLabel',{'MODEL2'});
2. I want to stack the bars graph for these two values, but i am not getting stacked bars. The larger bar need to be in red and smaller in blue. Thank you.
PErr = [235.6923 5.5125];
wd = 0.2;
bar(PErr,'stacked')

Best Answer

1.
bar(1, PErr(1),'FaceColor', 'r')
hold on
bar(2, PErr(2),'FaceColor', 'b')
set(gca, 'XTick', [1 2])
set(gca, 'XTickLabel', {'Model1' 'Model2'})
2. Stacked works only if you have more than one bar. So you have to add another fake bar using nan and adjust the x-axis
bar([PErr; nan(1,2)], 'stacked')
a = axis; axis([a(1) a(2)-0.8 a(3:4)]); % -0.8 selected such that it looks nice