Why does legend command fail after boxplots with hold command?

조회 수: 4 (최근 30일)
Daniel Bridges
Daniel Bridges 2017년 11월 28일
편집: Daniel Bridges 2017년 12월 5일
Following the MATLAB legend documentation example , the following code excerpt issues the warning and erroneous image displayed in the screenshot. Why? How do I create a legend?
cm3tomm3 = 10^3;
datatoplot = ExtractLessData(height(data),newvoldata,'blurred','rectum');
figure
boxplot(cm3tomm3*datatoplot.Volumes,datatoplot.DoseInterval,...
'colors','b','BoxStyle','filled')
title('Dosed Rectal Volumes (mm^3) per Dose Interval (Gy(RBE))')
hold on
datatoplot = ExtractLessData(height(data),newvoldata,'planned','rectum');
boxplot(cm3tomm3*datatoplot.Volumes,datatoplot.DoseInterval,...
'colors','r')
hold off
legend('planned: red, outlined', 'blurred: blue, filled')

답변 (1개)

Elizabeth Reese
Elizabeth Reese 2017년 11월 30일
Boxplots are groups of lines in the axes, so their legend functionality is limited. Mathworks' teams are aware of this and considering improving this in a future release.
You can still achieve this legend by saving the handles from the boxplots and then manually creating the legend.
For example:
load carsmall
% Make the plot
f = figure;
h1 = boxplot(MPG,Origin,'colors','b','BoxStyle','filled');
set(gca,'XTickLabel',{' '})
hold on
h2 = boxplot(gca,Acceleration,Origin,'colors','r');
hold off
% reset the axes limits to show the full boxplots
ylim([min(min(MPG),min(Acceleration)) max(max(MPG),max(Acceleration))])
legend([h1(2),h2(3)],'one, blue filled','two, red, outlined')
You will need to find which index in h1 and h2 correspond to lines in the box of your plot.
  댓글 수: 1
Daniel Bridges
Daniel Bridges 2017년 12월 5일
편집: Daniel Bridges 2017년 12월 5일
> You will need to find which index in h1 and h2 correspond to lines in the box of your plot.
How do I do this? I assigned the boxplots to variables as you did h1, h2 and my results are 4x19 and 7x19 double arrays. It's not clear what these arrays represent ( plot visualizes them as straight lines); I checked the boxplot documentation and it doesn't mention the case of assigning boxplot to a variable (i.e. h1 = boxplot(...)).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by