如何调整matplotlib图例框的大小? [英] How to adjust the size of matplotlib legend box?

查看:2680
本文介绍了如何调整matplotlib图例框的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图,其左上角非常空白.因此,我决定将我的图例框放在此处.

I have a graph whose left upper corner is quite blank. So I decide to put my legend box there.

但是,我发现图例中的项目很小,而传奇框本身也非常小.

However, I find the items in legend are very small and the legend box itself is also quite small.

小",我的意思是这样的

By "small", I mean something like this

如何使图例框中的项目(不是文本!)更大?

How can I make the items (not texts!) in the legend box bigger?

我如何使盒子本身更大?

推荐答案

要控制图例内部的填充(有效地使图例框变大),请使用borderpad kwarg.

To control the padding inside the legend (effectively making the legend box bigger) use the borderpad kwarg.

例如,这是默认设置:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left')
plt.show()

如果使用borderpad=2更改内部填充,我们将使整个图例框变大(单位是字体大小的倍数,类似于em):

If we change inside padding with borderpad=2, we'll make the overall legend box larger (the units are multiples of the font size, similar to em):

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left', borderpad=2)
plt.show()

或者,您可能想要更改项目之间的间距.使用labelspacing进行控制:

Alternately, you might want to change the spacing between the items. Use labelspacing to control this:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left', labelspacing=2)
plt.show()

但是,在大多数情况下,同时调整labelspacingborderpad最为有意义:

In most cases, however, it makes the most sense to adjust both labelspacing and borderpad at the same time:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left', borderpad=1.5, labelspacing=1.5)
plt.show()

另一方面,如果标记非常大,则可能需要增加图例中显示的行的长度.例如,默认值可能看起来像这样:

On the other hand, if you have very large markers, you may want to make the length of the line shown in the legend larger. For example, the default might look something like this:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 5)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, marker='o', markersize=20,
            label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left')
plt.show()

如果更改handlelength,则图例中的行会更长,看起来更真实. (我还在这里调整borderpadlabelspacing以便留出更多空间.)

If we change handlelength, we'll get longer lines in the legend, which looks a bit more realistic. (I'm also tweaking borderpad and labelspacing here to give more room.)

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 5)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, marker='o', markersize=20,
            label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left', handlelength=5, borderpad=1.2, labelspacing=1.2)
plt.show()

在文档中,以下是您可能想探索的其他一些选项:

From the docs, here are some of the other options you might want to explore:

Padding and spacing between various elements use following
keywords parameters. These values are measure in font-size
units. E.g., a fontsize of 10 points and a handlelength=5
implies a handlelength of 50 points.  Values from rcParams
will be used if None.

=====================================================================
Keyword       |    Description
=====================================================================
borderpad          the fractional whitespace inside the legend border
labelspacing       the vertical space between the legend entries
handlelength       the length of the legend handles
handletextpad      the pad between the legend handle and text
borderaxespad      the pad between the axes and legend border
columnspacing      the spacing between columns

这篇关于如何调整matplotlib图例框的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆