3rd time asking this question, still hopeful. How can I match my bar colors to my plots?
1 view (last 30 days)
Show older comments
Patrick Petre
on 30 Nov 2020
Edited: Cris LaPierre
on 30 Nov 2020
Im creating 5 plots in one loop. If I have 2 Sets of Data, I run the loop 2 times creating 10 plots. All 5 graphs from one set have the same color. In each loop i calc a value which i want to compare in a bar plot.
So if i have 2 sets i create : 1. Loop: 5 graphs and one bar (Blue)
2. Loop: 5 graphs and one bar (Red)
My problem is that the bar plot doesnt have the correct colors. They all just have the same color.
for i = 1:number
c = {'b','r','g','y','k'};
coefs = cell2mat(p(:));
subplot(4,1,4);
b{i} = bar(coefs(i,2)); <--- This just plots me the value of coefs(2,2) in c(2), so red. So i only have one bar.
set(b{i},'FaceColor',c{i});
I can change it to:
b{i} = bar(coefs(1:i,2)); <--- This now gives me both but ofc now they both have c(2), what makes sense.
set(b{i},'FaceColor',c{i});
0 Comments
Accepted Answer
Cris LaPierre
on 30 Nov 2020
Edited: Cris LaPierre
on 30 Nov 2020
It's not entirely clear to me what you want.
Do you want one bar plot, where each bar has a different color?
c = {'b','r','g','y','k'};
coeffs = randi(10,[1 5]); % Creates a single vector of data
for i = 1:length(coeffs)
hold on
bar(i,coeffs(1,i),c{i});
end
hold off
Or do you want multiple bar plots, where all the bars in each figure have the same color, and and the color is different in each figure?
c = {'b','r','g','y','k'};
coeffs = randi(10,[2 5]); % creates an array of data
figure
tiledlayout(1,2)
for i = 1:2
nexttile
bar(coeffs(i,:),'FaceColor',c{i})
end
2 Comments
Cris LaPierre
on 30 Nov 2020
Edited: Cris LaPierre
on 30 Nov 2020
You need to specify the x location of each bar. Otherwise, it uses the index of the bar height values. Where you are passing in a single height in each loop, x always is 1, and all the bars were being plotted on top of each other.
More Answers (0)
See Also
Categories
Find more on Subplots 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!