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!