MATLAB: How to create a legend with multiple colored squares

filled squareslegend

Hey,
how can I create a legend for the background of the following plot?
I would like to have a textbox with 3 colored squares (like the background) and the related category.
Thanks for your help!!

Best Answer

% Create an axe
hAxe = axes(...);
% Get your patches handles
hGreen = patch(hAxe, ...);
hYellow = patch(hAxe, ...);
hRed = patch(hAxe, ...);
% Add legend
legend(hAxe, [hGreen hYellow hRed], {'Green', 'Yellow', 'Red'})
Related Question