How to change (global) legend location in stackedplot?

7 次查看(过去 30 天)
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