mrsoltys
mrsoltys

Reputation: 1115

Matlab: Reduce the spacing between symbols and labels in the legend

When using the legend command in matlab, how can I reduce the horizontal distance between the legend symbols and their corresponding labels?

Example code:

Line1=plot(x1,y1,'s');
Line2=plot(x2,y2,'o');
Line3=plot(x3,y3,'^');
Leg=legend([Line1, Line2, Line3],...
           'Line1 text','Line2 text','Line3 text',...
           'Location','NorthEast');

Upvotes: 3

Views: 5805

Answers (2)

Mohsen Nosratinia
Mohsen Nosratinia

Reputation: 9864

You can find the children of Leg, search for the ones that have their Type set to text and relocate them. Here is a code to show how to do that. It moves them to left by 0.2 which is relative to the legend box.

ch = get(Leg, 'Children');
textCh = ch(strcmp(get(ch, 'Type'), 'text'));
for iText = 1:numel(textCh)
    set(textCh(iText), 'Position', get(textCh(iText), 'Position') + [-0.2 0 0])
end

Upvotes: 5

user2537472
user2537472

Reputation:

I am curious why you want to do this but a possible solution could be:

clf;
hold on;
x=0:0.1:2*pi;
plot(x,sin(x),'s');
plot(x,cos(x),'o');
ax=legend('sin','cos');
LEG = findobj(ax,'type','text');
set(LEG,'HorizontalAlignment','center')

You can test out 'center' and 'right' and use whichever works. If neither works, ignore my answer.

Upvotes: 0

Related Questions