3

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');
mrsoltys
  • 1,075
  • 10
  • 13

2 Answers2

5

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
Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52
0

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.

  • Thanks, Good Idea, but this causes the legend text to overlap with the symbols. I am just trying to improve the readability of my plot, i feel as it is currently, there is just too much space between the legend text and the symbols – mrsoltys Jul 01 '13 at 03:44