Plotting multiple bar graphs
162 次查看(过去 30 天)
显示 更早的评论
Hi everyone, I am plotting 3 different bar graphs on a same figure window. I have managed up to this point, shown in the figure. I want them to be side by side not overlapping. Any help guys ?
figure(5);
hold on;
bar(xdata,bpcombine3,0.125,'FaceColor',[0,0,1],...
'EdgeColor',[0,0,1]);
bar(xdata,bpcombine2,0.25,'FaceColor',[0,1,0],...
'EdgeColor',[0,1,0]);
bar(xdata,bpcombine1,0.4,'FaceColor',[1,0,0]);
set(gca, 'XTick', 1:6, 'XTickLabel', labels);
title('Blocking Probability vs Routing Level');
0 个评论
采纳的回答
Star Strider
2015-4-20
Guessing here because I don’t have all your data, but I would do something like this:
bpcombined = [bpcombine1(:), bpcombine2(:), bpcombine3(:)];
hb = bar(xdata, bpcombined, 'grouped')
You can then change the colours by referring to the individual bar series using the ‘hb’ handle. (The (:) creates each vector as a column vector so the concatenation works correctly.)
3 个评论
Star Strider
2015-4-20
I’m using synthetic data for illustration and to test my code, but this should work:
data = [randi([1 3], 5, 1) randi([4 6], 5, 1) randi([4 6], 5, 1)];
figure(1)
hb = bar(data)
set(hb(1), 'FaceColor','r')
set(hb(2), 'FaceColor','b')
set(hb(3), 'FaceColor','g')
Wenbin Hou
2017-7-12
The answer is 'Change properties for a specific bar series by indexing into the object array'(I am taking it from the help file).
Actually if you go to help document of 'bar', you will see this example:
y = [2 4 6; 3 4 5]; b = bar(y); Then if you want to change the color and width of the second column, you can do this: b(2).LineWidth = 2; b(2).EdgeColor = 'red';
更多回答(2 个)
Althaf V S
2018-12-8
figure()
% NOT an efficeint way,......BUT GOOD way to control all the elements of bar chart
set(gcf,'color','white');
ca = categorical({'SP5#1','SP5#2','SP6#Part 1B','SP6#Part 1C'});
bar(ca(1),sp51bs)
hold on
bar(ca(2),sp52bs)
hold on
bar(ca(3),maxbssp6)
hold on
bar(ca(4),maxbssp6c)
title('PART D: Compare BASE SHEAR ');
legend('SP5#1','SP5#2','SP6#Part 1B','SP6#Part 1C');
ylabel('BASE SHEAR (Kips)');
1 个评论
Franciszek Aniol
2022-3-7
I recommend this change to make it less tedious
ca = categorical({'SP5#1','SP5#2','SP6#Part 1B','SP6#Part 1C'});
value = [sp51bs sp52bs maxbssp6 maxbssp6c]
for n = 1:1:4 %could also use range of the vector called value as such 1:1:range(value)
bar(ca(n),value(n);
end
Al in St. Louis
2019-5-14
It's really frustrating that bar insists on making a stacked graph, and there is literally no way to change it to grouped without mucking around creating a new matrix.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!