How to change (global) legend location in stackedplot?
13 views (last 30 days)
Show older comments
Florian Berzsenyi
on 22 Jun 2023
Commented: Florian Berzsenyi
on 27 Jun 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.
0 Comments
Accepted Answer
Shree Charan
on 27 Jun 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];
More Answers (0)
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!