![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172269/image.png)
legend ignores leading whitespace, thus ignoring field width and messing up alignment
6 次查看(过去 30 天)
显示 更早的评论
Hi!
I am creating a legend where each entry has the same number of characters, padded if necessary by leading whitespace. When I pass this to "legend", this whitespace gets ignored and the "significant characters" centered. Does anyone know how I can force the legend to take the white space into account? Or simply right-align the text?
Mini-example showing the problem:
x = 0:0.1:5;
y = sin(x);
a = logspace(-3,0,10);
% create string array, fixing the field width
tmp = arrayfun(@(x)sprintf('%8.3g %s',x,'Shift'),a,'UniformOutput',0)
figure
for i = 1:length(a)
plot(x,y+exp(a(i))); hold on;
end
legend(tmp) % create legend
Thanks!
0 个评论
采纳的回答
the cyclist
2015-11-27
编辑:the cyclist
2015-11-27
You need to choose a fixed-width font. For example
h = legend(tmp) % create legend
set(h,'FontName','monospaced')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172269/image.png)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!