如何使用matplotlib.pyplot更改图例大小

时间:2023-02-07 23:41:33

Simple question here: I'm trying to get the size of my legend using matplotlib.pyplot to be smaller (i.e., the text to be smaller). The code I'm using goes something like this:

这里有一个简单的问题:我正在尝试使用matplotlib来获取图例的大小。pyplot要更小(即。,文本要更小)。我使用的代码是这样的:

plot.figure()
plot.scatter(k, sum_cf, color='black', label='Sum of Cause Fractions')
plot.scatter(k, data[:, 0],  color='b', label='Dis 1: cf = .6, var = .2')
plot.scatter(k, data[:, 1],  color='r',  label='Dis 2: cf = .2, var = .1')
plot.scatter(k, data[:, 2],  color='g', label='Dis 3: cf = .1, var = .01')
plot.legend(loc=2)

6 个解决方案

#1


359  

You can set an individual font size for the legend by adjusting the prop keyword.

您可以通过调整prop关键字来设置传奇的单个字体大小。

plot.legend(loc=2, prop={'size': 6})

This takes a dictionary of keywords corresponding to matplotlib.font_manager.FontProperties properties. See the documentation for legend:

这需要一个对应于matplotlib.font_manager的关键字字典。FontProperties属性。参见图例文档:

Keyword arguments:

关键字参数:

prop: [ None | FontProperties | dict ]
    A matplotlib.font_manager.FontProperties instance. If prop is a 
    dictionary, a new instance will be created with prop. If None, use
    rc settings.

It is also possible, as of version 1.2.1, to use the keyword fontsize.

从1.2.1版本开始,也可以使用关键字fontsize。

#2


41  

This should do

这个应该怎么做

import pylab as plot
params = {'legend.fontsize': 20,
          'legend.handlelength': 2}
plot.rcParams.update(params)

Then do the plot afterwards.

之后再做情节。

There are a ton of other rcParams, they can also be set in the matplotlibrc file.

还有很多其他的rcParams,它们也可以在matplotlibrc文件中设置。

Also presumably you can change it passing a matplotlib.font_manager.FontProperties instance but this I don't know how to do. --> see Yann's answer.

另外,您还可以更改它传递一个matplotlib.font_manager。FontProperties实例,但我不知道怎么做。- - >看到Yann的回答。

#3


29  

There are also a few named fontsizes, apart from the size in points:

除了以点为单位的大小之外,还有一些命名的字体大小:

xx-small
x-small
small
medium
large
x-large
xx-large

Usage:

用法:

pyplot.legend(loc=2, fontsize = 'x-small')

#4


21  

using import matplotlib.pyplot as plt

使用进口matplotlib。pyplot作为plt

Method 1: specify the fontsize when calling legend (repetitive)

方法1:在调用legend(重复)时指定fontsize

plt.legend(fontsize=20) # using a size in points
plt.legend(fontsize="x-large") # using a named size

With this method you can set the fontsize for each legend at creation (allowing you to have multiple legends with different fontsizes). However, you will have to type everything manually each time you create a legend.

使用此方法,您可以在创建时为每个图例设置fontsize(允许您拥有具有不同字体大小的多个图例)。但是,每次创建一个图例时都必须手动输入所有内容。

(Note: @Mathias711 listed the available named fontsizes in his answer)

(注:@Mathias711在他的答案中列出了可用的fontsize)

Method 2: specify the fontsize in rcParams (convenient)

方法2:在rcParams中指定字体大小(方便)

plt.rc('legend',fontsize=20) # using a size in points
plt.rc('legend',fontsize='medium') # using a named size

With this method you set the default legend fontsize, and all legends will automatically use that unless you specify otherwise using method 1. This means you can set your legend fontsize at the beginning of your code, and not worry about setting it for each individual legend.

使用此方法,您可以设置默认的legend fontsize,所有的传奇都会自动使用它,除非您使用方法1指定其他的。这意味着您可以在代码的开头设置legend fontsize,而不必为每个单独的legend设置它。

If you use a named size e.g. 'medium', then the legend text will scale with the global font.size in rcParams. To change font.size use plt.rc(font.size='medium')

如果你用一个命名的尺寸。'medium',然后传奇文本将与全局字体缩放。在rcParams大小。改变字体。大小使用plt.rc(font.size =“中”)

#5


14  

There are multiple settings for adjusting the legend size. The two I find most useful are:

有多个设置可以调整图例大小。我认为最有用的两个是:

  • labelspacing: which sets the spacing between label entries in multiples of the font size. For instance with a 10 point font, legend(..., labelspacing=0.2) will reduce the spacing between entries to 2 points. The default on my install is about 0.5.
  • 标签间距:将标签项之间的间距设置为字体大小的倍数。例如使用10点字体,legend(……),标签间距=0.2)将使条目之间的间距减少到2点。我安装的默认值是0.5。
  • prop: which allows full control of the font size, etc. You can set an 8 point font using legend(..., prop={'size':8}). The default on my install is about 14 points.
  • 可以完全控制字体大小等。你可以使用图例设置8点字体。支持= {“大小”:8 })。我安装的默认值是14点。

In addition, the legend documentation lists a number of other padding and spacing parameters including: borderpad, handlelength, handletextpad, borderaxespad, and columnspacing. These all follow the same form as labelspacing and area also in multiples of fontsize.

此外,图例文件还列出了许多其他的填充和间距参数,包括:边框、handlelength、handletextpad、borderaxespad和column间距。这些都遵循相同的形式,标签间距和面积也在数倍的字体大小。

These values can also be set as the defaults for all figures using the matplotlibrc file.

这些值也可以设置为使用matplotlibrc文件的所有数据的默认值。

#6


5  

On my install, FontProperties only changes the text size, but it's still too large and spaced out. I found a parameter in pyplot.rcParams: legend.labelspacing, which I'm guessing is set to a fraction of the font size. I've changed it with

在我的安装中,FontProperties只更改文本大小,但它仍然太大,并且间隔太长。我在pyplot中找到了一个参数。rcParams:传奇。标签间距,我猜应该是字体大小的一小部分。我改变了它

pyplot.rcParams.update({'legend.labelspacing':0.25})

I'm not sure how to specify it to the pyplot.legend function - passing

我不知道如何在pyplot中指定它。传说——传递函数

prop={'labelspacing':0.25}

or

prop={'legend.labelspacing':0.25}

comes back with an error.

返回时出现错误。

#1


359  

You can set an individual font size for the legend by adjusting the prop keyword.

您可以通过调整prop关键字来设置传奇的单个字体大小。

plot.legend(loc=2, prop={'size': 6})

This takes a dictionary of keywords corresponding to matplotlib.font_manager.FontProperties properties. See the documentation for legend:

这需要一个对应于matplotlib.font_manager的关键字字典。FontProperties属性。参见图例文档:

Keyword arguments:

关键字参数:

prop: [ None | FontProperties | dict ]
    A matplotlib.font_manager.FontProperties instance. If prop is a 
    dictionary, a new instance will be created with prop. If None, use
    rc settings.

It is also possible, as of version 1.2.1, to use the keyword fontsize.

从1.2.1版本开始,也可以使用关键字fontsize。

#2


41  

This should do

这个应该怎么做

import pylab as plot
params = {'legend.fontsize': 20,
          'legend.handlelength': 2}
plot.rcParams.update(params)

Then do the plot afterwards.

之后再做情节。

There are a ton of other rcParams, they can also be set in the matplotlibrc file.

还有很多其他的rcParams,它们也可以在matplotlibrc文件中设置。

Also presumably you can change it passing a matplotlib.font_manager.FontProperties instance but this I don't know how to do. --> see Yann's answer.

另外,您还可以更改它传递一个matplotlib.font_manager。FontProperties实例,但我不知道怎么做。- - >看到Yann的回答。

#3


29  

There are also a few named fontsizes, apart from the size in points:

除了以点为单位的大小之外,还有一些命名的字体大小:

xx-small
x-small
small
medium
large
x-large
xx-large

Usage:

用法:

pyplot.legend(loc=2, fontsize = 'x-small')

#4


21  

using import matplotlib.pyplot as plt

使用进口matplotlib。pyplot作为plt

Method 1: specify the fontsize when calling legend (repetitive)

方法1:在调用legend(重复)时指定fontsize

plt.legend(fontsize=20) # using a size in points
plt.legend(fontsize="x-large") # using a named size

With this method you can set the fontsize for each legend at creation (allowing you to have multiple legends with different fontsizes). However, you will have to type everything manually each time you create a legend.

使用此方法,您可以在创建时为每个图例设置fontsize(允许您拥有具有不同字体大小的多个图例)。但是,每次创建一个图例时都必须手动输入所有内容。

(Note: @Mathias711 listed the available named fontsizes in his answer)

(注:@Mathias711在他的答案中列出了可用的fontsize)

Method 2: specify the fontsize in rcParams (convenient)

方法2:在rcParams中指定字体大小(方便)

plt.rc('legend',fontsize=20) # using a size in points
plt.rc('legend',fontsize='medium') # using a named size

With this method you set the default legend fontsize, and all legends will automatically use that unless you specify otherwise using method 1. This means you can set your legend fontsize at the beginning of your code, and not worry about setting it for each individual legend.

使用此方法,您可以设置默认的legend fontsize,所有的传奇都会自动使用它,除非您使用方法1指定其他的。这意味着您可以在代码的开头设置legend fontsize,而不必为每个单独的legend设置它。

If you use a named size e.g. 'medium', then the legend text will scale with the global font.size in rcParams. To change font.size use plt.rc(font.size='medium')

如果你用一个命名的尺寸。'medium',然后传奇文本将与全局字体缩放。在rcParams大小。改变字体。大小使用plt.rc(font.size =“中”)

#5


14  

There are multiple settings for adjusting the legend size. The two I find most useful are:

有多个设置可以调整图例大小。我认为最有用的两个是:

  • labelspacing: which sets the spacing between label entries in multiples of the font size. For instance with a 10 point font, legend(..., labelspacing=0.2) will reduce the spacing between entries to 2 points. The default on my install is about 0.5.
  • 标签间距:将标签项之间的间距设置为字体大小的倍数。例如使用10点字体,legend(……),标签间距=0.2)将使条目之间的间距减少到2点。我安装的默认值是0.5。
  • prop: which allows full control of the font size, etc. You can set an 8 point font using legend(..., prop={'size':8}). The default on my install is about 14 points.
  • 可以完全控制字体大小等。你可以使用图例设置8点字体。支持= {“大小”:8 })。我安装的默认值是14点。

In addition, the legend documentation lists a number of other padding and spacing parameters including: borderpad, handlelength, handletextpad, borderaxespad, and columnspacing. These all follow the same form as labelspacing and area also in multiples of fontsize.

此外,图例文件还列出了许多其他的填充和间距参数,包括:边框、handlelength、handletextpad、borderaxespad和column间距。这些都遵循相同的形式,标签间距和面积也在数倍的字体大小。

These values can also be set as the defaults for all figures using the matplotlibrc file.

这些值也可以设置为使用matplotlibrc文件的所有数据的默认值。

#6


5  

On my install, FontProperties only changes the text size, but it's still too large and spaced out. I found a parameter in pyplot.rcParams: legend.labelspacing, which I'm guessing is set to a fraction of the font size. I've changed it with

在我的安装中,FontProperties只更改文本大小,但它仍然太大,并且间隔太长。我在pyplot中找到了一个参数。rcParams:传奇。标签间距,我猜应该是字体大小的一小部分。我改变了它

pyplot.rcParams.update({'legend.labelspacing':0.25})

I'm not sure how to specify it to the pyplot.legend function - passing

我不知道如何在pyplot中指定它。传说——传递函数

prop={'labelspacing':0.25}

or

prop={'legend.labelspacing':0.25}

comes back with an error.

返回时出现错误。