Legend Positioning in Tiled Chart Layout
67 次查看(过去 30 天)
显示 更早的评论
I have created 2 by 2 tiled chart and I want to position the legend centered above the figure. For some reason, MATLAB 2019b, which I am using, has locked the 'Position' property of the legend when usend in a tiled chart.
Firstly, why would they do this, and secondly does someone know a workaround?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271774/image.png)
Thanks,
Manuel
1 个评论
Kyle Johnson
2020-9-23
编辑:Kyle Johnson
2020-9-23
I have the same problem. I have even tried specifying the legend position to none.
回答(2 个)
Adam Danz
2020-9-23
" MATLAB 2019b ... has locked the 'Position' property of the legend when usend in a tiled chart."
Yes, that's correct.
Starting in r2020b the legend placement is more flexible and supports changes to the position propery as well as setting "location" relative to the tiled layout.
x = magic(4);
figure()
tiledlayout(2,2)
ax(1) = nexttile;
plot(x,'.','MarkerSize', 20)
ax(2) = nexttile;
bar(x,'stacked')
ax(3) = nexttile;
plot(x,'-','LineWidth', 2)
ax(4) = nexttile;
contour(x,'LineWidth',2)
ax(4).Colormap = lines(256);
lh =legend(ax(1),'Location','NorthOutside','Orientation','Horizontal');
lh.Layout.Tile = 'North'; % <----- relative to tiledlayout
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/365032/image.png)
6 个评论
Adam Danz
2020-9-23
In the past I've use tight_subplot from the file exchange to do what you're describing with regular subplots.
Kyle Johnson
2020-9-23
So this is really annoying. This is the workaround I developed. You can just create a new axis over the entire tiled layout, plot some NaNs there, and then set that axis to invisible. You then have a floating legend to reposition.
I made some room for that legend by adjusting the inner position of the tiled layout.
axs is an array containing all of the axes handles. t is the handle to the tiled layout.
var1name = 'annoyance (metric ton)';
var2name = 'frustration (ounces)';
ylabel(axs(1),var1name)
ylabel(axs(2),['mean: ' var1name])
ylabel(axs(3),var2name)
ylabel(axs(4),['mean: ' var2name])
xlabel(axs(3),'coding hour (hr)');
xlabel(axs(4),'coding hour (hr)');
t.InnerPosition = [t.InnerPosition(1) .15 t.InnerPosition(3) (t.InnerPosition(4)+t.InnerPosition(1)-.15)];
tmp = axes(fh1);
plot(tmp,nan,nan,'.','color',c(1,:))
hold on
plot(tmp,nan,nan,'.','color',c(2,:))
l = legend(tmp,{'attempt1','attempt2'},'Orientation','horizontal');
l.Position(1:2) = [.5-l.Position(3)/2 .025];
l.Title.String = 'Attempts';
tmp.Visible = 'off';
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/365035/image.png)
2 个评论
Daniel
2020-11-20
I think this is what I'm looking for, but I get an error when using InnerPosition with the handle for the tiledlayout. Any ideas?
Adam Danz
2020-11-20
You must be using a version of Matlab prior to r2020b as my answer explains.
This answer applies changes to InnerPosition to axes that are independent to tiledLayout.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!