Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Change length of legend line sample?

1,482 views
Skip to first unread message

drgz

unread,
Mar 12, 2010, 3:07:07 AM3/12/10
to
Is there any way in MATLAB to reduce the length of the line sample, as it is with i.e. Gnuplot (set key samplen 'sample length')?

When I search in the product help I find examples for changing almost everything else, but can't find anything about this.

drgz

unread,
Mar 12, 2010, 4:54:05 AM3/12/10
to

Walter Roberson

unread,
Mar 12, 2010, 5:06:57 AM3/12/10
to

Yes, it is _possible_ to change it, but there probably is no special
control for the length (I could figure out if there is, but it's 4 am
and I have to sleep.)

Legends in matlab are implemented by adding a second axis to the figure.
The lines and text are then placed within that axis. Find that second
axis and you can find its child line objects, and you can then change
their XData properties to change the length and positions of the lines.
Or more simply, make the axis smaller by changing its Position
properties, and then set the XLim property of the axis so that the
left-hand side of the lines are cut off. Saves you altering the
individual lines, and if you were wanting the lines shorter you were
probably wanting to change the axis Position anyhow (either because you
wanted a smaller legend box or because you wanted longer titles and it
would be a nuisance to also change the Position properties of all of the
text() objects to match a shorter line length...)

Sadik

unread,
Mar 12, 2010, 7:01:07 AM3/12/10
to
Hi,

Actually you can. But to do that, you have to find the handle of the line object in the legend.

An example:

plot(1:10)
legend('a')
linesInPlot = findobj('type','line'); % The second one is the line we are looking for
get(linesInPlot(2),'XData') % returns 0.1231 0.7385
set(linesInPlot(2),'XData',[0.1231 0.4]) % so that new length < 0.5*original, right?

That should be it.

Best.

"drgz " <syr...@hotmail.com> wrote in message <hncsnb$o11$1...@fred.mathworks.com>...

Sadik

unread,
Mar 12, 2010, 7:03:05 AM3/12/10
to
By the way, I am using Matlab 7, so you had better check if it is really the second element of linesInPlot, that is if the handle is given by linesInPlot(2).


"drgz " <syr...@hotmail.com> wrote in message <hnd2vt$6r$1...@fred.mathworks.com>...

Sadik

unread,
Mar 12, 2010, 7:09:05 AM3/12/10
to
I am sorry, it seems that my message did not show up for some reason. I am writing again:

An example:

plot(1:10)
legend('a')

linesInPlot = findobj('type','line'); % linesInPlot(2) is the handle to that line


get(linesInPlot(2),'XData') % returns 0.1231 0.7385

set(linesInPlot(2),'XData',[0.1231 0.4]) % so that new length < 0.5*previous

Best.


"Sadik " <sadik...@gmail.com> wrote in message <hndahp$3ij$1...@fred.mathworks.com>...

drgz

unread,
Mar 12, 2010, 12:27:05 PM3/12/10
to
"drgz " <syr...@hotmail.com> wrote in message <hnd2vt$6r$1...@fred.mathworks.com>...
> Is there any way in MATLAB to reduce the length of the line sample, as it is with i.e. Gnuplot (set key samplen 'sample length')?
>
> When I search in the product help I find examples for changing almost everything else, but can't find anything about this.

Thanks a lot to both of you! Hopefully there will an easier way of doing this in the future ;)

Greets!

arpan...@gmail.com

unread,
Mar 29, 2019, 9:29:06 AM3/29/19
to
The simplest way to do it is to use: -
leg = legend('Plot1','Plot2',...);
leg.ItemTokenSize = [x1,x2];
By default x1=30 and x2=18 so put larger or smaller numbers as x1,x2 to increase or decrease the legend line size.
0 new messages