MATLAB: XTick labels for bar plot of 2 data sets

barMATLABxticklabel

I want to make a bar chart with 2 data sets that have different number of values and I want each set to have the same color. Example code:
figure, bar([1:5],rand(1,5),'b'); hold on
bar([6:12],rand(1,7),'r')
The bar chart shows the XTickLabels for the first data set but not the second. How do I get XTickLabels for all 12 bars?
If I make a bar chart with a dataset with 12 entries, I can't change the colors of some bars, for example bars 1 – 5, so I had to break it up and make 2 bar plots.
Hope this makes sense.
Thanks!
blue_red_bar_chart.jpg

Best Answer

One way is to concatanate the 'XData' vectors of both series, and use that as the 'XData' value for the first (blue) bar series, then plot both series:
figure
hb1 = bar([1:5],rand(1,5),'b');
hb1.XData = 1:12;
hold on
hb2 = bar([6:12],rand(1,7),'r');
hold off
This also works even if the 'XData' vectors are not continuous:
figure
hb1 = bar([1:5],rand(1,5),'b');
hb1.XData = [1:5 7:13];
hold on
hb2 = bar([7:13],rand(1,7),'r');
hold off
EDIT —
Added plot:
XTick labels for bar plot of 2 data sets - 2019 01 30.png