# how to fill histogram legend markers

167 views (last 30 days)
sani on 24 Feb 2021
Answered: Lior de Marcas on 14 Aug 2021
hello,
I'm plotting using histograms, and when I insert legend into the plot, the rectangles are hollow.
I'd like them to be filled but couldn't find any documentation about how to do it.
is it even possible?

KALYAN ACHARJYA on 24 Feb 2021
Edited: KALYAN ACHARJYA on 24 Feb 2021
I tried to reproduce the same error, I did not get it
data1=randi(10,[1,100]);
data2=randi(10,[1,100]);
histogram(data1,'FaceColor','r');
hold on;
histogram(data2,'FaceColor','b');
legend('Data1','Data2')
Can you confirm it?
sani on 24 Feb 2021
so the only way is to find alternative way to display the data?

Lior de Marcas on 14 Aug 2021
Had a similar problem. Probabley not relevant anymore, but you can use "fake" bar chart in order to do that (unfortanatly I don't remember the first post I saw something simillar in to give the deserved credit):
% Plot some stairs histograms:
data1=randi(10,[1,100]);
data2=randi(10,[1,100]);
histogram(data1,'DisplayStyle','stairs');
hold on;
histogram(data2,'DisplayStyle','stairs');
% Get since we didn't specify colors, we use 'auto'. Get the colors from
% the axis color order
% If you specified colors, use them instead
colorList = get(gca,'ColorOrder');
% Now Create bar chart with nan so it won't show:
b = bar(nan(2,2)) %2X2 to create two bar charts (2 columns in the matrix == 2 bar charts).
b =
1×2 Bar array: Bar Bar
% The 2 rows is just so it won't create 1 chart for a vector input - there is probably a better way
set(b,{'FaceColor'},num2cell(colorList(1:2,:),2))
legend(b,{'Data1','Data2'})