How can I get a stacked bar graph with a single bar?
31 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
David McVea
el 15 de Jul. de 2016
Editada: Thiago de Aquino Costa Sousa
el 5 de Oct. de 2022
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.
0 comentarios
Respuesta aceptada
Star Strider
el 15 de Jul. de 2016
One possibility for a workaround:
v = [0.2 0.8];
figure;
bar(sum(v), 'y')
hold on
bar(v(1), 'b')
hold off
6 comentarios
André Bigalke
el 22 de Jun. de 2018
Editada: André Bigalke
el 22 de Jun. de 2018
Found a very good solution for big data: how-do-i-create-a-stacked-bar-plot-in-matlab-that-fix-certain-values-to-specific
just add 1 line to limit the x axis:
bar([1,2,3,4,5,6,7,8,9,10,11,12,13; nan(1,13)], 'Stacked');
set(gca,'xtick',1);
xlim([0.25 1.75]);
Martijn
el 9 de En. de 2019
Note that if you also actually specify x values and use a NaN there as well, you do not have to tweak the limits and ticks:
bar([1;nan], [1:3; nan(1,3)], 'stacked')
Más respuestas (1)
Daniel
el 11 de Feb. de 2020
Editada: Daniel
el 27 de Abr. de 2020
(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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271092/image.jpeg)
4 comentarios
Michaela Konstantinou
el 27 de Abr. de 2022
Do you maybe know how to remove the margins from left anr right? Thanks
Thiago de Aquino Costa Sousa
el 5 de Oct. de 2022
Editada: Thiago de Aquino Costa Sousa
el 5 de Oct. de 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.
Ver también
Categorías
Más información sobre Bar Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!