How to correspond line colors with histogram

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','')

 Accepted Answer

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 Comments

Thank you so much!
As always, my pleasure!
You can choose any colormap you want, or create your own. There are several options.
Do you maybe know how to remove the lines? I can't get them to be removed
If you are referring to the edge lines around the histogram bars, put this assignment after the histogram call:
m.EdgeColor = 'none';
If you are referring to something else, please provide a bit more detail.
Oh i'm sorry, that was a bit unclear. I meant the problem with the legend because it also displays the lines in it.
No worries!
In that event, it is simply necessary to keep track of the histogram handles, then pass them to the legend call, and lightly edit the legend call arguments.
The full code is now:
out = randn(1500,4).*[10 20 30 40]; % Create Data To Test Code
x = linspace(-80, 80); % Create Data To Test Code
cm = jet(size(out,2)); % Define ‘colormap’
[~,ErrorLength]= size(out);
figure
hold on
for i =1:ErrorLength
m{i} = histogram(out(:,i),'normalization','pdf')
m{i}.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’
end
hold off
xlabel('Orientation');
ylabel('Error rate');
legend([m{:}], 'Target 1','Target 2','Target 3','Target 4')
Note that ‘m’ is now ‘m{i}’, and they are included as the first argument of the lightly edited legend call. (I also updated my version of your code to make it a bit more efficient.) The edge lines around the histogram bars have returned as well.
Thank you very much! Ik worked with () instead of {}!
As always, my pleasure!
It worked for me with both. I used {} (cell array) to be safe.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!