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

发布于 2024-11-30 17:36:17 字数 425 浏览 4 评论 0原文

这里有一个简单的问题:我试图使用 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)

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:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

场罚期间 2024-12-07 17:36:17

您可以通过调整 prop 关键字来设置图例的单独字体大小。

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

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

关键字参数:

属性:[无|字体属性 |字典]
    matplotlib.font_manager.FontProperties 实例。如果 prop 是 
    字典,将使用 prop 创建一个新实例。如果没有,则使用
    RC 设置。

也是可能的,从版本 1.2.1 开始,使用关键字fontsize

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

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:

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.

2024-12-07 17:36:17

using import matplotlib.pyplot as plt

方法 1:调用图例时指定字体大小(重复)

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

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

(注:@Mathias711在他的回答中列出了可用的命名字体大小)

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

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

使用此方法,您可以设置默认的图例字体大小,所有图例将自动使用它,除非您可以使用方法 1 来指定。这意味着您可以在代码开头设置图例字体大小,而不必担心为每个单独的图例设置它。

如果您使用命名大小,例如'medium',则图例文本将根据rcParams 中的全局font.size 进行缩放。要更改 font.size,请使用 plt.rc(font.size='medium')

using import matplotlib.pyplot as plt

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

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.

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

Method 2: specify the fontsize in rcParams (convenient)

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.

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')

千鲤 2024-12-07 17:36:17

应该这样做

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

,然后再做情节。

还有大量其他 rcParams,它们也可以在 matplotlibrc 文件中设置。

另外,大概您可以通过传递 matplotlib.font_manager.FontProperties 实例来更改它,但我不知道该怎么做。 -->看看Yann的回答。

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.

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.

ㄟ。诗瑗 2024-12-07 17:36:17

除了以磅为单位的大小之外,还有一些名为 fontsize 的字体

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

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

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')
羁绊已千年 2024-12-07 17:36:17

现在,在 2021 年,使用 matplotlib 3.4.2,您可以设置图例字体,

plt.legend(title="My Title", fontsize=10, title_fontsize=15)

其中 fontsize 是图例中项目的字体大小,title_fontsize 是图例的字体大小标题。更多信息请参见 matplotlib 文档

Now in 2021, with matplotlib 3.4.2 you can set your legend fonts with

plt.legend(title="My Title", fontsize=10, title_fontsize=15)

where fontsize is the font size of the items in legend and title_fontsize is the font size of the legend title. More information in matplotlib documentation

み青杉依旧 2024-12-07 17:36:17

有多种设置可用于调整图例大小。我发现最有用的两个是:

  • labelspacing:它以字体大小的倍数设置标签条目之间的间距。例如,对于 10 磅字体,legend(..., labelspacing=0.2) 会将条目之间的间距减少到 2 磅。我安装的默认值约为 0.5。
  • prop:允许完全控制字体大小等。您可以使用 legend(..., prop={'size':8}) 设置 8 点字体。我安装的默认值约为 14 点。

此外,图例文档列出了许多其他填充和间距参数包括:borderpadhandlelengthhandletextpadborderaxespad列间距。这些都遵循与标签间距和区域相同的形式,也是字体大小的倍数。

还可以使用 matplotlibrc 文件 将这些值设置为所有图形的默认值。

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.
  • 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.

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.

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

淡莣 2024-12-07 17:36:17

在我的安装中,FontProperties 仅更改文本大小,但它仍然太大且间隔较大。我在 pyplot.rcParams 中找到了一个参数:legend.labelspacing,我猜它被设置为字体大小的一小部分。我已经更改了它,

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

我不确定如何将其指定给 pyplot.legend 函数 - 传递

prop={'labelspacing':0.25}

prop={'legend.labelspacing':0.25}

返回错误。

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

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

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

prop={'labelspacing':0.25}

or

prop={'legend.labelspacing':0.25}

comes back with an error.

清风无影 2024-12-07 17:36:17
plot.legend(loc = 'lower right', decimal_places = 2, fontsize = '11', title = 'Hey there', title_fontsize = '20')
plot.legend(loc = 'lower right', decimal_places = 2, fontsize = '11', title = 'Hey there', title_fontsize = '20')
绻影浮沉 2024-12-07 17:36:17

您可以减小图例大小设置:

plt.legend(labelspacing=y, handletextpad=x,fontsize)  

labelspacing 是每个标签之间的垂直间距。

handletextpad 是实际图例和您的图例之间的距离
标签。

字体大小是不言自明的

you can reduce the legend size setting:

plt.legend(labelspacing=y, handletextpad=x,fontsize)  

labelspacing is the vertical space between each label.

handletextpad is the distance between the actual legend and your
label.

And fontsize is self-explanatory

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文