Remove whitespace in Legend with Latex Labels
7 次查看(过去 30 天)
显示 更早的评论
Hi, does anyone know why there is so much whitespace after each entry in the legend here? I've tried to shrink the legend width by doing:
pos = legend.Position;
pos(3) = 0.5*pos(3);
legend.Position = pos;
But this hasn't worked. I've attached the code below.
Any help would be appreciated.

Here is the code below: (I'm sure there is a better way to write the legend names out, but I'm lazy and this was good enough for my purposes).
f = figure;
hold on
plot(Cl, Cm(:,1),"-bsquare")
plot(Cl, Cm(:,3),"-bdiamond")
plot(Cl, Cm(:,5),"-b^")
plot(Cl, Cm(:,7),"-bv")
plot(Cl, Cm(:,2),"-rsquare")
plot(Cl, Cm(:,4),"-rdiamond")
plot(Cl, Cm(:,6),"-r^")
plot(Cl, Cm(:,8),"-rv")
hold off
xlim([-1.1, 1.4])
leg = legend({'$x_{cg}=0.18\bar{c}, \delta_e = 0\degree $','$x_{cg}=0.22\bar{c}, \delta_e = 0\degree $','$x_{cg}=0.26\bar{c}, \delta_e = 0\degree $','$x_{cg}=0.30\bar{c}, \delta_e = 0\degree $','$x_{cg}=0.18\bar{c}, \delta_e = 5\degree $','$x_{cg}=0.22\bar{c}, \delta_e = 5\degree $','$x_{cg}=0.26\bar{c}, \delta_e = 5\degree $','$x_{cg}=0.30\bar{c}, \delta_e = 5\degree $'},'Interpreter','latex', 'Location','northeast', 'FontSize',11);
% pos = leg.Position;
% pos(3) = 0.25*pos(3); % Tried this, didn't work
% leg.Position = pos;
grid on
ax = gca;
ax.GridLineWidth = 1.5;
ax.XAxis.MinorTick = 'on';
ax.XMinorGrid = 'on';
ax.YAxis.MinorTick = 'on';
ax.YMinorGrid = 'on';
f.Units = 'pixels';
f.Position = [10,10,990,490];
2 个评论
采纳的回答
dpb
2025-9-23
编辑:dpb
2025-9-23
Cl=linspace(-1,1.4).';
Cm=linspace(4,3.2); Cm=repmat(Cm,8,1).';
Cm=Cm+[0 -0.075 -0.2 -0.255 -0.4 -0.455 -0.6 -0.655];
f=figure('Units','pixels','Position',[10,10,990,490]);
hold on
plot(Cl, Cm(:,1),"-bsquare")
plot(Cl, Cm(:,3),"-bdiamond")
plot(Cl, Cm(:,5),"-b^")
plot(Cl, Cm(:,7),"-bv")
plot(Cl, Cm(:,2),"-rsquare")
plot(Cl, Cm(:,4),"-rdiamond")
plot(Cl, Cm(:,6),"-r^")
plot(Cl, Cm(:,8),"-rv")
hold off
xlim([-1.1, 1.4]), ylim([2.2 4.2])
leg = legend({'$x_{cg}=0.18\bar{c}, \delta_e = 0^{o}$', ...
'$x_{cg}=0.22\bar{c}, \delta_e = 0^{o}$', ...
'$x_{cg}=0.26\bar{c}, \delta_e = 0^{o}$', ...
'$x_{cg}=0.30\bar{c}, \delta_e = 0^{o}$', ...
'$x_{cg}=0.18\bar{c}, \delta_e = 5^{o}$', ...
'$x_{cg}=0.22\bar{c}, \delta_e = 5^{o}$', ...
'$x_{cg}=0.26\bar{c}, \delta_e = 5^{o}$', ...
'$x_{cg}=0.30\bar{c}, \delta_e = 5^{o}$'}, ...
'Interpreter','latex', 'Location','southwest', 'FontSize',11);
grid on
ax = gca;
ax.GridLineWidth = 1.5;
ax.XAxis.MinorTick = 'on';
ax.XMinorGrid = 'on';
ax.YAxis.MinorTick = 'on';
ax.YMinorGrid = 'on';
The MATLAB LaTeX interpreter doesn't like the \degree because it is in a package and not base LaTeX.
Prior releases warn of bad syntax although they don't give any klew as to what it is that isn't kosher so it's a witch hunt to track down what it doesn't like. (Although anything that is shown as being in some package will fail so anything that needs a package will not work in MATLAB, so there's where to start).
Alternative is a superscript "o"; this has been a subject of angst for years...
ADDENDUM
LaTeX uses circles so an alternative could be
p=[0.18:0.04:0.30].'; p=[p;p];
d=[zeros(4,1); 5*ones(4,1)];
lgds=compose('$x_{cg}=%.2f\\bar{c}, \\delta_e = %d^{\\circ}$',p,d);
which is accepted as it doesn't need to load a package as with \degree or \textdegree
6 个评论
dpb
2025-9-24
编辑:dpb
2025-9-24
No problem, glad to help.
What I had failed to research before, however, is that it appears the \degree symbol has been added and <the documentation has been updated to include it> in current release; this table after the line starting with vdots is new. I simply didn't think there was any likelihood of such additions having been made and so didn't look at current documentation.
But it does appear that there are still some warts in using at least some of them.
I'll amend my recommendation to one of this is a bug with a new feature and a formal bug report should be filed.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

