Remove whitespace in Legend with Latex Labels

5 ビュー (過去 30 日間)
Daniel
Daniel 2025 年 9 月 23 日
コメント済み: dpb 2025 年 9 月 25 日
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 件のコメント
Walter Roberson
Walter Roberson 2025 年 9 月 23 日
For testing, could you attach a .mat that defines Cl and Cm ?
Daniel
Daniel 2025 年 9 月 23 日
編集済み: Daniel 2025 年 9 月 23 日
Hi, is a csv okay? The first column is Cl, the rest are Cm:
So importing should be:
dat = readmatrix("Cl_Cm.csv");
Cl = dat(:,1);
Cm = dat(:,2:9);

サインインしてコメントする。

採用された回答

dpb
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
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.
dpb
dpb 2025 年 9 月 25 日
ADDENDUM
I did go ahead and submitted the bug report.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

製品


リリース

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by