问题描述
我正在Matlab中创建一个绘图,其中包括一些线和一个填充.例如
I'm creating a plot in matlab that includes some lines, as well as a fill. For example,
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 sample 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 doesn't seem to work for the fill. How do I change the size of the fill sample in the legend?
推荐答案
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)
您可以通过设置Position
属性来操纵图例的大小.但是,默认情况下,图例似乎适合其内容,因此您可以将其放大,但不能缩小(尝试使用鼠标调整其大小):
You can manipulating the size of the legend by setting the Position
property. However, it seems that the legend fits its content as tight as possible by default, so you can make it bigger, but not smaller (try resizing it with the mouse):
p = get(h,'Position'); p(3)=0.1;
set(h, 'Position',p);
另一种方法是减小用于标签的字体大小:
Another way is to reduce the font size used for the labels:
h = legend('fill', 'line one', 'line two', 'line three')
set(h, 'FontSize',6); %# do this before changing the other stuff
这篇关于在Matlab图例中更改填充样本的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!