필터 지우기
필터 지우기

Creating a Tabular Legend

조회 수: 47 (최근 30일)
Ted Baker
Ted Baker 2020년 4월 22일
댓글: Ted Baker 2020년 4월 22일
I would like to create a legend with an additional column for a number to be inserted next to the variable name as follows:
However, I'm unsure as to how I can achieve this layout - I have tried searching for a function like gridlegend, but I cannot get it to work in this format. Any ideas as to how I can get this implemented? Thanks in advance.

채택된 답변

Sindar
Sindar 2020년 4월 22일
Not a good solution, but with some fiddling gets pretty close:
myleg=compose('%-8s%10d',["Boot";"Engine";"Wheel"],[1 3 5]');
myl=legend(myleg)
title(myl,sprintf('%9s %-12s%s','Line','Location','Number'))
  댓글 수: 3
Sindar
Sindar 2020년 4월 22일
I saw that and couldn't figure out why it's happening. You can tweak the formatspec in the compose and sprintf.
'%-8s%10d'
says left-justify a string, padding the length to at least 8. Then, (right-justify) a number padding to length at least 10
'%9s %-12s%s'
says (right-justify) a string, pad to 9; three spaces; left-justify a string, pad to 12; (right-justify) a string, don't pad
Ted Baker
Ted Baker 2020년 4월 22일
Thanks, I managed to manually line it up by adding thin spaces as padding (U+2009).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 4월 22일
Try this
ax = axes();
hold(ax);
plot(rand(1,10), 'LineWidth', 2);
plot(rand(1,10), 'LineWidth', 2);
plot(rand(1,10), 'LineWidth', 2);
labels1 = {'boot', 'engine', 'wheel'};
labels2 = {'1', '3', '5'};
labels = cellfun(@(x,y) {sprintf('%10s%9s', x, y)}, labels1, labels2);
l = legend(labels, ...
'FontName', 'FixedWidth');
titles = {'Line', 'Location', 'Number'};
titles = sprintf('%4s%14s%10s', titles{:});
l.Title.String = titles;

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by