필터 지우기
필터 지우기

How to change (global) legend location in stackedplot?

조회 수: 11 (최근 30일)
Florian Berzsenyi
Florian Berzsenyi 2023년 6월 22일
댓글: Florian Berzsenyi 2023년 6월 27일
I want to move the location of the "global" legend from top ('north') to the side ('west') in a stackedplot. The location of the legends in the subplots can be changed by calling
stackplot.AxesProperties(1).LegendLocation = 'north'; %east, west, ...
However the property LegendLocation is not available for the global legend on top and outside of all subplots. Basically, I want to move the legend from the top to the left side and change the legend orientation from horizontal (side-by-side) to vertical (top-to-bottom).
stackplot.LegendOrientation = 'vertical'; % labels are arranged top-to-bottom, legend stay at the top (north)
stackplot.LegendLocation = 'east'; % Unrecognized property 'LegendLocation' for class 'matlab.graphics.chart.StackedLineChart'.
How can I modifiy the properties of the global legend? the stackedPlot object (StackedLineChart) does not have any Childrens (an not Children() property), and I cannot find a property pointing to the handle of the legend box.

채택된 답변

Shree Charan
Shree Charan 2023년 6월 27일
Hi Florian,
The location of the legend can be set using the “LegendLocation” name value parameter in the “stackedplot” function as shown in the example below.
% Assume indoors and outdoors as two variables loaded from .mat
stackedplot(indoors,outdoors, "LegendLocation", "West");
However, “LegendLocation” is not available as a parameter of “stackedplot”. To change the legend location, a workaround as shown in the below code snippet could be used.
load indoors; load outdoors;
tl = tiledlayout(2,1);
nexttile;
h = stackedplot(indoors,outdoors,'LegendVisible','off');
ax = nexttile;
co = colororder;
hold on
plot(ax,NaN,NaN,'LineStyle','none','Marker','square','MarkerFaceColor',co(1,:));
plot(ax,NaN,NaN,'LineStyle','none','Marker','square','MarkerFaceColor',co(2,:));
hold off
ax.Visible = 'off';
l = legend(ax,h.LegendLabels);
l.Layout.Tile = 'West';
tl.GridSize_I = [1 1];

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by