Change the fill size in the Matlab legend

I am creating a plot in matlab that includes some lines as well as a fill. For instance,

fill([0 1 1], [0 1 0], [.9 .9 .9]);
plot(rand(5, 1), 'b');
plot(rand(5, 1), 'r');
plot(rand(5, 1), 'g');
legend('fill', 'line one', 'line two', 'line three');

I can change the length of the lines in the legend with:

f = findobj('type', 'line');
set(f(2), 'XData', [.2, .3]); % Changes line three
set(f(4), 'XData', [.2, .3]); % Changes line two
set(f(6), 'XData', [.2, .3]); % Changes line one

But this method does not seem to work for filling. How to resize a fill pattern in a legend?

+3
source share
3 answers
fill([0 1 1], [0 1 0], [.9 .9 .9]); hold on
plot(rand(5, 1), 'b');
plot(rand(5, 1), 'r');
plot(rand(5, 1), 'g'); hold off
h = legend('fill', 'line one', 'line two', 'line three');

%# find handles of lines inside legend that have a non-empty tag
hLegendLines = findobj(h, 'type', 'line', '-and', '-regexp','Tag','[^'']');
set(hLegendLines, 'XData', [.2, .3])

%# find handle of patch inside legend
hLegendPatch = findobj(h, 'type', 'patch');
set(hLegendPatch, 'XData', [.2, .2, .3, .3])

EDIT : (response to comments)

You can manipulate the size of the legend by setting the property Position. Nevertheless, it seems that the legend is as large as possible in terms of content, so you can make it bigger, but not smaller (try changing it with the mouse):

p = get(h,'Position'); p(3)=0.1;
set(h, 'Position',p);

alt text

Another way is to reduce the font size used for labels:

h = legend('fill', 'line one', 'line two', 'line three')
set(h, 'FontSize',6);    %# do this before changing the other stuff

alt text

+4

MATLAB R2014b ( HG2) -, , , .

, " " LegendIcon, Icon LegendEntry . LegendEntry Legend, EntryContainer.

LegendIcon Transform, , , . , Matrix, x .

, :

hLegend = findobj('Type','legend');
entries = hLegend.EntryContainer.Children;
for entry = entries'
    entry.Icon.Transform.Matrix(1) = entry.Icon.Transform.Matrix(1) / 2;
end

, , , . , , reset.

+1

. , , , . . PDF ( "PaperPositionMode" "" , - ...), PDF ... ? Matlab 2015b

,

0

Source: https://habr.com/ru/post/1774218/


All Articles