I am plotting my data using subplot. since the legend is a bit long, I want to put a horzontal legend in top of each two sub plots.I can create a legend for each subplot but don't know how to create one at the top of each two plots. I apperciate your help.
x=1:80;
y=rand(15,80);
j=1;
legend={'LC1','LC2','LC3','LC4','LC5','LC6','LC7'};
for ii=1:4
for i=1:4
subplot(2,2,i)
hold on;
row = 0;
row = row+1;
plot(x(j:j+4),y(row,j:j+4))
end
j=j+5;
end

 채택된 답변

Hassan
Hassan 2011년 5월 6일

0 개 추천

I found another way. First I created a subplot with a legend, then placed the legend on the appropriate place on the plot, I created a M-code from the plot and got the position of the legend, then used the position for all subplots. . . .
end
leg1=legend(legend);
set(leg1,'Position',[p1 p2 p3 p4]);
j=j+5;
end

댓글 수: 2

Dan Po
Dan Po 2016년 9월 30일
편집: Dan Po 2016년 9월 30일
subplot(121);
peaks
legend('peaks');
subplot(122);
plot(1:10);
legend('1:10')
Simon Mwakitabu
Simon Mwakitabu 2024년 1월 16일
This results to individual legend on each subplot. Not a common one on top of the figure including all plots.
New versions of Matlab since 2019 has tiled_layout with incredible features.

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

추가 답변 (2개)

Adam Danz
Adam Danz 2020년 9월 29일

3 개 추천

Update
Since this thread continues to get 500+ views per month 9 years later, here's an updated solution for Matlab r2020b or later.
Using TiledLayout, legends can be positioned relative to figure edges and can contain graphics objects from different subplots or tiles.
Examples:
Laura Proctor
Laura Proctor 2011년 5월 5일

0 개 추천

You cannot have a legend that pulls data from more than one subplot. However, you can plot all the data in one subplot, then set the visibility to off and create a legend that will capture everything. Here's an example:
figure
subplot(211)
plot(1:10)
hold on
hi = plot(sin(1:10),'mx-');
legend('one','two','Location','NorthOutside')
set(hi,'Visible','Off')
subplot(212)
plot(sin(1:10),'mx-');

댓글 수: 6

Hassan
Hassan 2011년 5월 5일
thanks Laura for the comment. the legend is the same for all the plots and I can not put the all plots in one subplot since it becomes very small. Maybe the way you said is the way but I still don't know how to use your code for my work.
Kelly Kearney
Kelly Kearney 2011년 5월 6일
You cannot have a legend that pulls data from more than one subplot
Kelly Kearney
Kelly Kearney 2011년 5월 6일
Hmm, my comment got cut off. I mean to quote that statement, then say... Sure you can [label multiple subplots in one legend]. Just pass the handles of the lines/objects to be labeled.
Hassan
Hassan 2011년 5월 6일
all my subplots have the same axis and legend, so I just found the right position for the legend and then used it for all subplots.
Johanna
Johanna 2023년 9월 6일
편집: Adam Danz 2023년 9월 6일
If I use the solution proposed by Laura Proctor, the word "two" in the legend is somewhat transparent even if I try to force it to be black. Any ideas?
figure
subplot(211)
plot(1:10)
hold on
hi = plot(sin(1:10),'mx-');
h_legend= legend('one','two','Location','NorthOutside')
h_legend =
Legend (one, two) with properties: String: {'one' 'two'} Location: 'northoutside' Orientation: 'vertical' FontSize: 7.2000 Position: [0.4556 0.8493 0.1235 0.0704] Units: 'normalized' Show all properties
set(h_legend,'TextColor','k')
set(hi,'Visible','Off')
subplot(212)
plot(sin(1:10),'mx-');
Adam Danz
Adam Danz 2023년 9월 6일
It's because of this line
set(hi,'Visible','Off')
You are setting the 2nd line in the first axes to Visible=off so it is not displayed. Therefore, it's grayed out in the legend.
Instead, use object handles to specify what should go in the legend.

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

카테고리

태그

질문:

2011년 5월 5일

댓글:

2024년 1월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by