How can I get a stacked bar graph with a single bar?
Mostra commenti meno recenti
Hello,
I am trying to get a single bar using the stacked property:
figure; bar([0.2 0.8], 'stacked')
This does not create a single stacked bar, it creates two bars of height 0.2 and 0.8.
A work-around is:
figure;bar([0.2 0.8; 1 1],'stacked'); set(gca,'xlim',[0.5 1.5])
But this seems like a silly thing to have to do.
Does anyone know why the stacked input doesn't seem to work for a single bar?
Thanks.
Risposta accettata
Più risposte (1)
(requires MATLAB 2019b or later)
bar(1,[0.2 0.8], 'stacked')
Use the bar(x,y) synatax instead of bar(y). This way you can control if the bars are grouped.

4 Commenti
Steffen Julius Wieting
il 27 Apr 2020
I tried this an got this ERROR:
bar(1,[0.2 0.8], 'stacked')
% Error using bar (line 172)
% X must be same length as Y.
Daniel
il 27 Apr 2020
I wasn't aware when writing the answer, but this works only in MATLAB 2019b or later.
Michaela Konstantinou
il 27 Apr 2022
Do you maybe know how to remove the margins from left anr right? Thanks
Thiago de Aquino Costa Sousa
il 5 Ott 2022
Modificato: Thiago de Aquino Costa Sousa
il 5 Ott 2022
@Daniel Do you know how to overlap the series instead of summing then? If you see your bar graph it is summing your data series, and stacking them. I would like to have the same graph but with the time series overlapped to highlight the difference between the serires. Thank you.
Categorie
Scopri di più su Bar Plots in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!