MATLAB中不正确的图例框大小?(Improper legend box size in MATLAB?)

我遇到了一个问题,即字体大小更改时图例框不会自动调整大小。

x = [1 4 6 8 0 2 4 7 8]

plot(x)

lngd = legend('Nov 5, 2010 09:00 - 09:01');

set(lngd, 'interpreter','latex', 'fontsize', 10);

生成以下图表

b426d63a909a431f4a00cfd4411e8f13.png

我的两个问题是

盒子尺寸太小。 这些词是开箱即用的。 我试过set(lngd, 'interpreter','latex', 'fontsize', 10, [0 0 10 10]); 放大盒子,但没有运气。

我希望将图例框移到右下角。 我试过set(lngd, 'southeast', 'interpreter','latex', 'fontsize', 10); ,但没有运气。

我怎么能解决这两个问题呢?

I encountered a problem where the legend box is not automatically sized as the font size changes.

x = [1 4 6 8 0 2 4 7 8]

plot(x)

lngd = legend('Nov 5, 2010 09:00 - 09:01');

set(lngd, 'interpreter','latex', 'fontsize', 10);

generates the following graph

b426d63a909a431f4a00cfd4411e8f13.png

My two problems are

The box size is too small. The words are out of box. I have tried set(lngd, 'interpreter','latex', 'fontsize', 10, [0 0 10 10]); to enlarge the box, but no luck.

I wish to move the legend box to the right bottom. I have tried set(lngd, 'southeast', 'interpreter','latex', 'fontsize', 10);, but no luck.

How may I fix these two problems?

原文:https://stackoverflow.com/questions/22471972

更新时间:2019-09-26 11:17

最满意答案

你有没有特别的理由使用'interpreter', 'latex' ? 对于某些MATLAB来说,它似乎与传说的拳击混乱。 您可以尝试通过设置“ Position变量来手动更改图例框的位置和大小。

x = [1 4 6 8 0 2 4 7 8]

plot(x)

lngd = legend('Nov 5, 2010 09:00 - 09:01');

set(lngd, 'fontsize', 10, 'interpreter','latex','Position', [0.55,0.15,0.35,0.08]);

Position向量是[left,bottom,width,height]框的0~1范围。

Is there any particular reason that you use 'interpreter', 'latex'? It seems to mess with the boxing of the legend for some MATLAB. You can try to manually change the legend box position and size by setting the Position variable.

x = [1 4 6 8 0 2 4 7 8]

plot(x)

lngd = legend('Nov 5, 2010 09:00 - 09:01');

set(lngd, 'fontsize', 10, 'interpreter','latex','Position', [0.55,0.15,0.35,0.08]);

The Position vector is [left,bottom,width,height] of the box in 0~1 range.

2014-03-18

相关问答

如果你命名你的legend ,你可以迭代它所包含的行。 例如: leg=plt.legend(bbox_to_anchor=(1.005, 1), loc=2, borderaxespad=0.)

for l in leg.get_lines():

l.set_alpha(1)

l.set_marker('.')

请注意,您还必须重新设置标记。 我建议设置它. 而不是在这里让它更加明显 If you name your legend, you can then iterate

...

您不需要plot和errorbar 。 errorbar功能正在使用绘图绘制在循环中先前绘制的线。 然后将图例应用于这些隐藏线以及可见线。 摆脱plot并使用errorbar(x, diam(r,:), diamerr(r,:))代替。 You don't need both plot and errorbar. The errorbar function is drawing over the line plotted earlier in the loop with plot. The leg

...

您可以添加另一个子图作为空区域,仅保留图例 ,关闭轴可见性,绘制线的'YData'为nan值,因此它们不会渲染: figure(1);

hSub = subplot(511); plot(1, nan, 1, nan, 'r'); set(hSub, 'Visible', 'off');

subplot(512); plot(T, a, T, a2, 'r'); grid; ylabel('p (°/s)');

subplot(513); plot(T, b, T, b2, 'r'); grid;

...

您可以添加任何前32个ASCII代码(不可打印的字符)来创建空间。 不知道它会与psfrag一起工作。 这里,这段代码使用ASCII代码3创建30个空格。 h_plot = plot([0 1], [0 1]);

h_legend = legend([ 'A' repmat(char(3),1,30) ],0);

编辑 另一种可能。 您可以使用图例中的手柄。 在这里,将图例的文本从10个字符更改为1个字符不会修改图例框的大小。 [~,OBJH,~,~] = legend('0123456789

...

我认为你需要在你的情节中“隐藏”你不想要的元素,只留下传说。 例如, clear all; close all;

figure;

p1 = plot([1:10], [1:10], '+-');

hold on;

p2 = plot([1:10], [1:10]+2, 'o--');

legend('text1', 'text2');

set(p1, 'visible', 'off');

set(p2, 'visible', 'off');

set(gca, 'visible', 'off'

...

你有没有特别的理由使用'interpreter', 'latex' ? 对于某些MATLAB来说,它似乎与传说的拳击混乱。 您可以尝试通过设置“ Position变量来手动更改图例框的位置和大小。 x = [1 4 6 8 0 2 4 7 8]

plot(x)

lngd = legend('Nov 5, 2010 09:00 - 09:01');

set(lngd, 'fontsize', 10, 'interpreter','latex','Position', [0.55,0.15,0.35

...

在matlab条形图中对图例颜色反向排序的答案中提供的代码在您的情况下不起作用的原因是因为在这种情况下( bar图表)图例中的对象是patches而在您的图中它们是lines 。 FaceColor仅适用于补丁而不适用于lines 。 解决问题的最简单方法应该是颠倒你从“开始”开始绘制线条的顺序,然后直接使用从色彩图中提取的colormap 。 尽管如此,如果您在绘制图形后想要使用legend ,除了在legend旋转项目之外还要更改图中线条的颜色,如果要使用从色彩图中提取的colormap集合(

...

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

hLege

...

好的,我现在明白你的问题。 我编写了一些数据: n = 8;

x = [1:n];

y(:,1) = rand(1,n);

y(:,2) = rand(1,n);

然后使用您的绘图命令: h = bar(x,y,'stacked');

hL = legend ((h([1 2])), {'North', 'South'});

但是,不要关闭盒子,只需将边缘颜色设置为白色: set(hL, 'EdgeColor', 'w')

然后使用: newPosition = [0.75 0.75 0.1

...

如果我理解正确,您需要访问legend调用的icons输出,并修改作为这些图标的子项的修补程序对象的MarkerSize属性。 致电legend : [h,icons,plots,legend_text] = legend('Circle', 'Plus', 'X', 'Location', 'NorthEast');

icons是一个6x1图形数组,如下所示: icons =

6x1 graphics array:

Text (Circle)

Text (P

...

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐