I could be misunderstanding your question, but I suspect the problem you are facing is that your bar graphs are perfectly overlapping one another. If you create two bar graphs with the same x-values, they will overlap each other and one could be hidden behind the other. The better approach is to create both bars simulteneously with a single call to the bar command.
Consider these three blocks of code. The second approach is preferred.
Overlapping Bars
x = 1:5;
y1 = 1:5;
y2 = 2:6;
bar(x,y1);
hold on
bar(x,y2);
Grouped Bars (Preferred Approach)
x = 1:5;
y1 = 1:5;
y2 = 2:6;
bar(x,[y1;y2]);
This approach can be used if the x-data for both bars is different as well, either by padding the x-values and y-values with NaNs, or passing to matrices into the bar command. If you pass two matrices into the bar command, be careful of the orientation of the matrices, or you will get unexpected results.
x1 = 1:5;
x2 = 3:7;
y1 = 1:5;
y2 = 2:6;
bar([x1; x2]',[y1;y2]');
Manually Shifted Bars
x = 1:5;
y1 = 1:5;
y2 = 2:6;
bar(x-0.25,y1,0.5)
hold on
bar(x+0.25,y2,0.5)
2 Comments
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/725247-how-do-i-combine-two-bar-graphs#comment_1281647
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/725247-how-do-i-combine-two-bar-graphs#comment_1281647
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/725247-how-do-i-combine-two-bar-graphs#comment_1282432
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/725247-how-do-i-combine-two-bar-graphs#comment_1282432
Sign in to comment.