필터 지우기
필터 지우기

Is there a command in MATLAB for creating one overall legend when I have a figure with subplots?

조회 수: 454 (최근 30일)
Is there a command in MATLAB for creating one overall legend when I have a figure with subplots?
I have a figure with subplots and I would like to create one legend that refers to all of my subplots. Is there a way to do this?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2022년 8월 16일
편집: MathWorks Support Team 2022년 8월 17일
You can create an overall legend by first using 'tiledlayout' to create your subplots. Then, generate the legend and specify what should appear in the legend using a vector of graphics object handles. The position of the legend can be set by its 'Layout' property. Find an example of this workflow below.
figure()
tcl = tiledlayout(2,2);
nexttile(tcl)
line1 = plot(1:10,rand(1,10),'b','DisplayName','Data Axes 1');
title('Axes 1');
nexttile(tcl)
line2 = plot(1:10,rand(1,10),'g','DisplayName','Data Axes 2');
title('Axes 2');
nexttile(tcl)
line3 = plot(1:10,rand(1,10),'r','DisplayName','Data Axes 3');
title('Axes 3');
nexttile(tcl)
line4 = plot(1:10,rand(1,10),'c','DisplayName','Data Axes 4');
title('Axes 4');
% Construct a Legend with the data from the sub-plots
hL = legend([line1,line2,line3,line4]);
% Move the legend to the right side of the figure
hL.Layout.Tile = 'East';
For for information on the 'Layout' property, refer to this page: 
For MATLAB versions prior to MATLAB R2019b or code that uses 'subplot' instead of 'tiledlayout', there is no straight-forward way to create an overall legend. A workaround is to create an extra subplot, or an additional row or column, and use that space for the legend. Here is an example that uses a 2-by-2 grid of subplots with a third column reserved for the legend.
figure()
subplot(2,3,1)
line1 = plot(1:10,rand(1,10),'b','DisplayName','Data Axes 1');
title('Axes 1');
subplot(2,3,2)
line2 = plot(1:10,rand(1,10),'g','DisplayName','Data Axes 2');
title('Axes 2');
subplot(2,3,4)
line3 = plot(1:10,rand(1,10),'r','DisplayName','Data Axes 3');
title('Axes 3');
subplot(2,3,5)
line4 = plot(1:10,rand(1,10),'c','DisplayName','Data Axes 4');
title('Axes 4');
% Create a tile on the right column to get its position
ax = subplot(2,3,3,'Visible','off');
axPos = ax.Position;
delete(ax)
% Construct a Legend with the data from the sub-plots
hL = legend([line1,line2,line3,line4]);
% Move the legend to the position of the extra axes
hL.Position(1:2) = axPos(1:2);
You may use the 'Position' property of the legend to relocate it, for example, to adjust its vertical position.
  댓글 수: 2
Jonathan Kwang
Jonathan Kwang 2016년 11월 17일
I've tried the accepted answer in MATLAB R2016a and it still appears to work fine on my end.
If you are still having an issue with this, you can contact MathWorks Technical Support by using the following link:
https://www.mathworks.com/support/contact_us/
Eric Sargent
Eric Sargent 2020년 12월 9일
Starting in R2019a, you can use tiledlayout to create an "overall legend" effect. The code below attaches a legend to the second axes but places it outside of the layout.
tiledlayout(2,2, 'TileSpacing', 'compact')
nexttile
line1 = plot(1:10,rand(1,10),'b', 'DisplayName', 'Data Axes 1');
title('Axes 1');
nexttile
line2 = plot(1:10,rand(1,10),'g', 'DisplayName', 'Data Axes 2');
title('Axes 2');
nexttile
line3 = plot(1:10,rand(1,10),'r', 'DisplayName', 'Data Axes 3');
title('Axes 3');
nexttile
line4 = plot(1:10,rand(1,10),'c', 'DisplayName', 'Data Axes 4');
title('Axes 4');
% Create a Legend with the data from multiple axes
lg = legend(nexttile(2), [line1,line2,line3,line4]);
lg.Location = 'northeastoutside';

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

추가 답변 (1개)

Eric Sargent
Eric Sargent 2020년 12월 9일
Starting in R2019a, you can use tiledlayout to create an "overall legend" effect. The code below attaches a legend to the second axes but places it outside of the layout.
tiledlayout(2,2, 'TileSpacing', 'compact')
nexttile
line1 = plot(1:10,rand(1,10),'b', 'DisplayName', 'Data Axes 1');
title('Axes 1');
nexttile
line2 = plot(1:10,rand(1,10),'g', 'DisplayName', 'Data Axes 2');
title('Axes 2');
nexttile
line3 = plot(1:10,rand(1,10),'r', 'DisplayName', 'Data Axes 3');
title('Axes 3');
nexttile
line4 = plot(1:10,rand(1,10),'c', 'DisplayName', 'Data Axes 4');
title('Axes 4');
% Create a Legend with the data from multiple axes
lg = legend(nexttile(2), [line1,line2,line3,line4]);
lg.Location = 'northeastoutside';

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by