Overlay barchart with subplots
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi , i am trying to generate a overlay subploted bar chart with this data combined1 and combined 2
I want to see the different columns overlayed in the subplot with diferent colors but now i can only see the figure attached
combined1 = rand(10,5)
combined2 = rand(10,5)
ret_combined = [combined1];
ret_combined2 = [combined2];
f = figure; ax = axes(f); hold(ax, 'on')
subplot(2,1,1);
arrayfun(@(x1) bar(ret_combined(:, x1), 'stacked'), 1:5);
subplot(2,1,2);
arrayfun(@(x1) bar(ret_combined2(:, x1), 'stacked'), 1:5);
ax = gca;
0 commentaires
Réponse acceptée
Dave B
le 22 Oct 2021
Modifié(e) : Dave B
le 22 Oct 2021
Currently your code creates an axes, turns hold on, but then subplot creates a new axes that deletes 'ax'
You can add a hold on after subplot if you want to make multiple bars in the same axes, but the way bar interprets multiple stacked bars is to stack all of the values...so your code is equivalent to:
combined1 = rand(10,5);
combined2 = rand(10,5);
%ret_combined = [combined1]; % this line does nothing
%ret_combined2 = [combined2]; % this line does nothing
f = figure;
%ax = axes(f); hold(ax, 'on') %this line does thing
subplot(2,1,1);
% could put a hold on here to hold the axes...
% arrayfun(@(x1) bar(ret_combined(:, x1), 'stacked'), 1:5);
% but the result would be equivalent to
bar(combined1,'stacked');
subplot(2,1,2);
%arrayfun(@(x1) bar(ret_combined2(:, x1), 'stacked'), 1:5);
bar(combined2,'stacked');
If you're looking for a different image, can you describe a bit what you're looking for? 'stacked' is one way of combining multiple bar charts, but I think you're looking for a different sort of 'overlay'?
2 commentaires
Dave B
le 23 Oct 2021
Do you want the stacked combined1 on top of the stacked combined2?
combined1 = rand(10,5);
combined2 = rand(10,5);
bar(combined1,'stacked','FaceAlpha',.2);
hold on
bar(combined2,'stacked','FaceAlpha',.2);
colororder([hot(5);cool(5)])
Or do you want the 5 bars overlayed in each subplot?
figure
nexttile
for i = 1:5
bar(combined1(:,i),'FaceAlpha',.2);
hold on
end
nexttile
for i = 1:5
bar(combined2(:,i),'FaceAlpha',.2);
hold on
end
I don't know how you'd see anything in these pictures...maybe you're planning on adjusting the BarWidth?
figure
combined1 = rand(10,5);
combined2 = rand(10,5);
bar(combined1,'stacked');
hold on
bar(combined2,'stacked','BarWidth',.2);
colororder([hot(5);cool(5)])
Or do you want them in 3d:
figure
b1=bar3(combined1','stacked');
hold on
b2=bar3(combined2','stacked');
for i = 1:numel(b2)
b2(i).XData=b2(i).XData+1;
end
axis tight equal
set([b1 b2],'FaceAlpha',.3)
colormap lines
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Subplots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!