How to correspond line colors with histogram

7 ビュー (過去 30 日間)
MadjeKoe
MadjeKoe 2020 年 10 月 15 日
コメント済み: Star Strider 2020 年 10 月 15 日
I made these overlapping histograms using a for loop. I'm trying to get the lines in the same color as the histograms. Does anybody know how to do this?
[~,ErrorLength]= size(out);
for i =1:ErrorLength
hold on
m = histogram(out(:,i),'normalization','pdf');
f = fitdist(out(:,i),'kernel');
hoi = pdf(f,x);
l = line(x,hoi,'linestyle','-','linewidth',2);
hold off
end
xlabel('Orientation');
ylabel('Error rate');
legend('Target 1','','Target 2','','Target 3','','Target 4','')

採用された回答

Star Strider
Star Strider 2020 年 10 月 15 日
編集済み: Star Strider 2020 年 10 月 15 日
The FaceColor property of the bars is 'auto', so if you want the colors to be the same, it is necessary to define them:
out = randn(500,3).*[10 20 30]; % Create Data To Test Code
x = linspace(-80, 80); % Create Data To Test Code
cm = jet(size(out,2)); % Define ‘colormap’
[~,ErrorLength]= size(out);
for i =1:ErrorLength
hold on
m = histogram(out(:,i),'normalization','pdf')
m.FaceColor = cm(i,:); % Define ‘FaceColor’
f = fitdist(out(:,i),'kernel');
hoi = pdf(f,x);
l = line(x,hoi,'linestyle','-','linewidth',2, 'Color',cm(i,:)); % Define Line ‘Color’
hold off
end
xlabel('Orientation');
ylabel('Error rate');
legend('Target 1','','Target 2','','Target 3','','Target 4','')
You will need to fix the problem with the legend.
EDIT — (15 Oct 2020 at 12:17)
Added ‘out’ and ‘x’ definitions.
  8 件のコメント
MadjeKoe
MadjeKoe 2020 年 10 月 15 日
Thank you very much! Ik worked with () instead of {}!
Star Strider
Star Strider 2020 年 10 月 15 日
As always, my pleasure!
It worked for me with both. I used {} (cell array) to be safe.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by