简体   繁体   English

如何调整matplotlib图例框的大小

[英]How to adjust the size of matplotlib legend box

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?如何使图例框中的项目(不是文本! )更大?

How can i make the box itself bigger?我怎样才能使盒子本身更大?

To control the padding inside the legend (effectively making the legend box bigger) use the borderpad kwarg.要控制图例内部的填充(有效地使图例框变大),请使用borderpad kwarg。

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 labelspacing to 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 labelspacing and borderpad at the same time:然而,在大多数情况下,同时调整labelspacingborderpad是最有意义的:

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.如果我们改变handlelength长度,我们会在图例中得到更长的线条,看起来更真实一些。 (I'm also tweaking borderpad and labelspacing here to give more room.) (我还在此处调整borderpadlabelspacing以提供更多空间。)

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

When you call legend you can use the prop argument with a dict containing size.当您调用 legend 时,您可以将prop参数与包含大小的 dict 一起使用。

plt.errorbar(x, y, yerr=err, fmt='-o', color='k', label = 'DR errors')
plt.legend(prop={'size':50})

Eg例如更改图例大小

See here for more info on legend有关图例的更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM