How to change (global) legend location in stackedplot?

9 ビュー (過去 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];
  1 件のコメント
Florian Berzsenyi
Florian Berzsenyi 2023 年 6 月 27 日
Thank you, I will try it out! Looks simple enough.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by