![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/529854/image.png)
how to fill histogram legend markers
83 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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?![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/529834/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/529834/image.jpeg)
0 comentarios
Respuestas (2)
KALYAN ACHARJYA
el 24 de Feb. de 2021
Editada: KALYAN ACHARJYA
el 24 de Feb. de 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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/529854/image.png)
Can you confirm it?
3 comentarios
Steven Lord
el 24 de Feb. de 2021
The stairs DisplayStyle draws only the edges of the histogram bars, so the legend also shows only the edges. I don't believe there's a way to make it draw the filled boxes from the default DisplayStyle in the legend while using the stairs DisplayStyle in the axes.
Lior de Marcas
el 14 de Ag. de 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
% (https://www.mathworks.com/matlabcentral/answers/221889-how-do-i-get-the-facecolor-of-a-histogram-if-set-to-auto).
% 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).
% 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'})
0 comentarios
Ver también
Categorías
Más información sobre Histograms en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!