3rd time asking this question, still hopeful. How can I match my bar colors to my plots?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Cris LaPierre
2020-11-30
编辑:Cris LaPierre
2020-11-30
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 个评论
Cris LaPierre
2020-11-30
编辑:Cris LaPierre
2020-11-30
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!