How do I make one legend on a figure with multiple subplots?

조회 수: 13 (최근 30일)
Ian
Ian 2011년 1월 20일
I would like to make one legend on a figure with multiple subplots, and place it either at the top-middle, or align the legend just above one of my subplots.
Right now I have given up using the 'legend' command from matlab and have implemented using the 'axes' command. I then take away the Xticks and Yticks, and then just manually make my legend using the 'rectangle' and 'text' commands. The only issue with this is that because 'axes' uses [left bottom width height], and 'width' and 'height' are 'percentages' of the figure, I find that when I 'print' or 'save', that my self-made legend does not appear where it should...
One idea I had:
Is it possible to set 'position' in pixels by getting handle to the 'subplots' with respect to my 'figure'? In which case I could set the 'axes' on the figure with respect to the X position of the subplot, and then move it up above the top of the subplot so that my legend is not inside the subplot.
  댓글 수: 1
Todd Flanagan
Todd Flanagan 2011년 1월 20일
Hi Ian, I moved your answer into a comment on my answer. That's a good place for that sort of back and forth.

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

답변 (1개)

Todd Flanagan
Todd Flanagan 2011년 1월 20일

Something like this?

subplot(4,3,kdx);
plotmystuff(); % uses subplots 4-12 and leaves 1-3 empty
legend1 = legend(gca,'show');
% set the legend where I really want it.
set(legend1,...
          'Position',[a b c d]);

I typically run it one time and then use "File->Generate Code" to give me the actual coordinates I was satisfied with.

  댓글 수: 1
Todd Flanagan
Todd Flanagan 2011년 1월 20일
Ian says, "Yes, That is a good way of overcoming this challenge."

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

카테고리

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