Adding a global legend to a tiledlayout
Good morning,
I’m using MATLAB R2020a Update 2. I have a tiledlayout of five (three by two) area plots and would like to use the sixth, currently empty, tile to add a global legend. I’ve already found this question, and understand that there’s no official, built-in way of doing this, but perhaps it’s possible to get creative.
What I’ve tried is adding a new, empty area plot to the sixth tile, using NaNs as the shares, then adding a legend to that and setting the axis object for that tile to invisible, as follows:
% …
ax = nexttile;
area([NaN NaN], NaN(2, 4));
leg = legend({‘tar’, ‘sta’, ‘ext’, ‘dmp’);
leg.Location = ‘none’;
leg.Interpreter = ‘latex’;
leg.FontSize = 16;
ax.Visible = false;
This works in principle, but leaves a horizontal line where the X axis would be:
This only happens when I use area(), not e.g. plot(). Perhaps it’s a bug, perhaps it’s intentional and due to the way area() works. In any case it’s not what I want, but I can’t get rid of it (without breaking other things in the process).
I had the idea of making the children of this axis, i.e. the areas of the area plot, invisible as well, like so:
for i = 1:length(ax.Children)
ax.Children(i).Visible = false;
end
But while this gets rid of the horizontal line it also grays out the legend entries:
This behavior in turn is known and expected and apparently cannot be changed. The workaround suggested by a Mathworks staffer in the linked question is to plot NaNs; but that’s what I’m already doing and what’s leaving the horizontal line, due to area()’s quirks.
Can anyone help?
I’m not hung up on specifically creating an invisible area(), this is merely the best (first, only) idea I had for fudging a global legend. If anyone can make this approach work, that’d be wonderful. If anyone has another idea of how to achieve a similar effect, that’d be wonderful as well.
Thank you very much!Good morning,
I’m using MATLAB R2020a Update 2. I have a tiledlayout of five (three by two) area plots and would like to use the sixth, currently empty, tile to add a global legend. I’ve already found this question, and understand that there’s no official, built-in way of doing this, but perhaps it’s possible to get creative.
What I’ve tried is adding a new, empty area plot to the sixth tile, using NaNs as the shares, then adding a legend to that and setting the axis object for that tile to invisible, as follows:
% …
ax = nexttile;
area([NaN NaN], NaN(2, 4));
leg = legend({‘tar’, ‘sta’, ‘ext’, ‘dmp’);
leg.Location = ‘none’;
leg.Interpreter = ‘latex’;
leg.FontSize = 16;
ax.Visible = false;
This works in principle, but leaves a horizontal line where the X axis would be:
This only happens when I use area(), not e.g. plot(). Perhaps it’s a bug, perhaps it’s intentional and due to the way area() works. In any case it’s not what I want, but I can’t get rid of it (without breaking other things in the process).
I had the idea of making the children of this axis, i.e. the areas of the area plot, invisible as well, like so:
for i = 1:length(ax.Children)
ax.Children(i).Visible = false;
end
But while this gets rid of the horizontal line it also grays out the legend entries:
This behavior in turn is known and expected and apparently cannot be changed. The workaround suggested by a Mathworks staffer in the linked question is to plot NaNs; but that’s what I’m already doing and what’s leaving the horizontal line, due to area()’s quirks.
Can anyone help?
I’m not hung up on specifically creating an invisible area(), this is merely the best (first, only) idea I had for fudging a global legend. If anyone can make this approach work, that’d be wonderful. If anyone has another idea of how to achieve a similar effect, that’d be wonderful as well.
Thank you very much! Good morning,
I’m using MATLAB R2020a Update 2. I have a tiledlayout of five (three by two) area plots and would like to use the sixth, currently empty, tile to add a global legend. I’ve already found this question, and understand that there’s no official, built-in way of doing this, but perhaps it’s possible to get creative.
What I’ve tried is adding a new, empty area plot to the sixth tile, using NaNs as the shares, then adding a legend to that and setting the axis object for that tile to invisible, as follows:
% …
ax = nexttile;
area([NaN NaN], NaN(2, 4));
leg = legend({‘tar’, ‘sta’, ‘ext’, ‘dmp’);
leg.Location = ‘none’;
leg.Interpreter = ‘latex’;
leg.FontSize = 16;
ax.Visible = false;
This works in principle, but leaves a horizontal line where the X axis would be:
This only happens when I use area(), not e.g. plot(). Perhaps it’s a bug, perhaps it’s intentional and due to the way area() works. In any case it’s not what I want, but I can’t get rid of it (without breaking other things in the process).
I had the idea of making the children of this axis, i.e. the areas of the area plot, invisible as well, like so:
for i = 1:length(ax.Children)
ax.Children(i).Visible = false;
end
But while this gets rid of the horizontal line it also grays out the legend entries:
This behavior in turn is known and expected and apparently cannot be changed. The workaround suggested by a Mathworks staffer in the linked question is to plot NaNs; but that’s what I’m already doing and what’s leaving the horizontal line, due to area()’s quirks.
Can anyone help?
I’m not hung up on specifically creating an invisible area(), this is merely the best (first, only) idea I had for fudging a global legend. If anyone can make this approach work, that’d be wonderful. If anyone has another idea of how to achieve a similar effect, that’d be wonderful as well.
Thank you very much! tiledlayout, legend MATLAB Answers — New Questions