How to change (global) legend location in stackedplot?

15 vues (au cours des 30 derniers jours)
Florian Berzsenyi
Florian Berzsenyi le 22 Juin 2023
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.

Réponse acceptée

Shree Charan
Shree Charan le 27 Juin 2023
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 commentaire
Florian Berzsenyi
Florian Berzsenyi le 27 Juin 2023
Thank you, I will try it out! Looks simple enough.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by