Python 如何调整matplotlib图例框的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20048352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to adjust the size of matplotlib legend box?
提问by Sibbs Gambling
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 legendare very small and the legend box itselfis also quite small.
但是,我发现图例中的项目非常小,图例框本身也非常小。
By "small", I mean something like this
通过“小”,我的意思是这样的


How can I make the items (not texts!) in the legend box bigger?
如何使图例框中的项目(不是文本!)变大?
How can i make the box itself bigger?
我怎样才能使盒子本身更大?
采纳答案by Joe Kington
To control the padding inside the legend (effectively making the legend box bigger) use the borderpadkwarg.
要控制图例内的填充(有效地使图例框更大),请使用borderpadkwarg。
For example, here's the default:
例如,这是默认值:
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()


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):
如果我们用 改变内边距borderpad=2,我们将使整个图例框更大(单位是字体大小的倍数,类似于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()


Alternately, you might want to change the spacing between the items. Use labelspacingto control this:
或者,您可能想要更改项目之间的间距。使用labelspacing来控制这样的:
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()


In most cases, however, it makes the most sense to adjust both labelspacingand borderpadat the same time:
在大多数情况下,但是,这是很有道理的同时调整labelspacing和borderpad在同一时间:
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()


If we change handlelength, we'll get longer lines in the legend, which looks a bit more realistic. (I'm also tweaking borderpadand labelspacinghere to give more room.)
如果我们更改handlelength,我们将在图例中获得更长的线条,这看起来更真实一些。(我也在调整borderpad和labelspacing这里给更多的空间。)
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

