how do i combine two bar graphs
189 views (last 30 days)
Show older comments
Diego Monsave
on 23 Jan 2021
Edited: Benjamin Kraus
on 30 May 2024
I have tried to combine these two graphics in many ways but nothing works for me, just as the creation of the legends gives me an error. I need help for this because neither the combination nor the legends want to serve me, Thank you!
b1=bar(app.CasosUIAxes,data1.dateRep,data1.cases);
hold on
%grafica de barras 2
b2=bar(app.CasosUIAxes,data2.dateRep,data2.cases);
hold off
legend(app.CasosUIAxes,[b1 b2],'Bar Chart 1','Bar Chart 2')
2 Comments
Accepted Answer
Benjamin Kraus
on 8 Feb 2021
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); % Second bar ignores first bar and will overlap the first bar
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/513447/image.png)
Grouped Bars (Preferred Approach)
x = 1:5;
y1 = 1:5;
y2 = 2:6;
bar(x,[y1;y2]); % Creating both bars at once, they are aware of one another and will not overlap
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/513452/image.png)
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]');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/513457/image.png)
Manually Shifted Bars
x = 1:5;
y1 = 1:5;
y2 = 2:6;
bar(x-0.25,y1,0.5) % Manually shift the bar and make it narrower so the other bar fits.
hold on
bar(x+0.25,y2,0.5) % Manually shift the bar and make it narrower so the other bar fits.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/513462/image.png)
6 Comments
Benjamin Kraus
on 30 May 2024
Edited: Benjamin Kraus
on 30 May 2024
@Artjom Vartanyan: The "Grouped Bars (Preferred Approach)" above won't work with yyaxis. You could leverage the "Manually Shifted Bars" approach, but there is another option which is a bit more complicated.
For yyaxis, you need to create 4 different bar objects using two separate calls to the bar command:
- A bar object with the data for the left side.
- A bar object with all NaN values as a placeholder on the left side.
- A bar object with all NaN values as a placeholder on the right side.
- A bar object with the data for the right side.
Consider this example:
x = 1:5;
y1 = 1:5;
y2 = (1:5)*100;
yleft = [y1' NaN(5,1)];
yright = [NaN(5,1) y2'];
yyaxis left
bar(x,yleft);
yyaxis right
bar(x,yright);
More Answers (0)
See Also
Categories
Find more on Line 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!