Is there a command in MATLAB for creating one overall legend when I have a figure with subplots?
조회 수: 567 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2013년 5월 1일
편집: MathWorks Support Team
2022년 8월 17일
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
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
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
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
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';
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!