I created a boxplot and would like to add a legend to it, but when I call legend I get the following warning message and there is no legend in the plot figure:
Warning: Plot empty. > In legend at 286
Is it possible to add a legend to a box plot? My code is:
figure; colors = [1 0 0; 1 0 0; 0 0 1; 0 0.5 0; 0 0.5 0; 0 0.5 0]; x = boxplot(rand(100,6),'Colors',colors); legend('Group A','Group B','Group C')
figure; colors = [1 0 0; 1 0 0; 0 0 1; 0 0.5 0; 0 0.5 0; 0 0.5 0]; x = boxplot(rand(100,6), 'Colors',colors); % findall is used to find all the graphics objects with tag "box", i.e. the box plot hLegend = legend(findall(gca,'Tag','Box'), {'Group A','Group B','Group C'});
Depending on the number of variables that you have, you might not obtain the right color for each legend element. Use the following trick to manually change the color of each legend element:
% Among the children of the legend, find the line elements hChildren = findall(get(hLegend,'Children'), 'Type','Line'); % Set the horizontal lines to the right colors set(hChildren(6),'Color',[1 0 0]) set(hChildren(4),'Color',[0 0 1]) set(hChildren(2),'Color',[0 0.5 0])
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.