简体   繁体   English

如何设置 Matplotlib 轴图例的字体大小?

[英]How to set font size of Matplotlib axis Legend?

I have a code like this:我有这样的代码:

import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from matplotlib.font_manager import FontProperties

fontP = FontProperties()
fontP.set_size('xx-small')
fig=plt.figure()
ax1=fig.add_subplot(111)
plot([1,2,3], label="test1")
ax1.legend(loc=0, ncol=1, bbox_to_anchor=(0, 0, 1, 1),
           prop = fontP,fancybox=True,shadow=False,title='LEGEND')
plt.show()

图例字体大小

It can be seen in the plot that the setting in Fontsize does not affect the Legend Title font size.在 plot 中可以看到 Fontsize 中的设置不影响 Legend Title 字体大小。

How to set the font size of the legend title to a smaller size?如何将图例标题的字体大小设置为较小的大小?

This is definitely an old question, but was frustrating me too and none of the other answers changed the legend title fontsize at all, but instead just changed the rest of the text.这绝对是一个老问题,但也让我感到沮丧,其他答案都没有改变图例标题字体大小,而是改变了文本的其余部分。 So after banging my head against the matplotlib documentation for awhile I came up with this.因此,在我对 matplotlib 文档猛烈抨击一段时间后,我想到了这个。

legend = ax1.legend(loc=0, ncol=1, bbox_to_anchor=(0, 0, 1, 1),
           prop = fontP,fancybox=True,shadow=False,title='LEGEND')

plt.setp(legend.get_title(),fontsize='xx-small')

As of Matplotlib 3.0.3, you can also set it globally with从 Matplotlib 3.0.3 开始,您还可以使用以下命令对其进行全局设置

plt.rcParams['legend.title_fontsize'] = 'xx-small'

Here is how to change the fontsize of the legend list and/or legend title:以下是更改图例列表和/或图例标题的字体大小的方法:

legend=plt.legend(list,loc=(1.05,0.05), title=r'$\bf{Title}$') #Legend: list, location, Title (in bold)
legend.get_title().set_fontsize('6') #legend 'Title' fontsize
plt.setp(plt.gca().get_legend().get_texts(), fontsize='12') #legend 'list' fontsize

Banged my head against it too, here is another more flowing way of doing it:我的头也撞到了它,这是另一种更流畅的方法:

leg = ax.legend()
leg.set_title('A great legend',prop={'size':14})

我不知道如何为单个情节设置它,但我总是在全局范围内进行设置:

plt.rc('legend',**{'fontsize':6})

Inspired by the current top answer, I found a slightly more natural way to change the fontsizes in the legend.受当前最佳答案的启发,我找到了一种更自然的方法来更改图例中的字体大小。 The fontsize argument sets the font size of each of the data labels, and the title_fontsize argument sets the fontsize of the title, if you give the legend a title. fontsize参数设置每个数据标签的字体大小,而title_fontsize参数设置标题的字体大小,如果你给图例一个标题。

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0,1,2],[2,1,2],label='test_data (fs=12)')
ax.legend(fontsize=12, title='TITLE (fs=30)',title_fontsize=30)

Gives a figure like this:给出这样的图:
在此处输入图片说明

这是最快的:

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

I generally do in this way.我一般都是这样的。 Once the plot has been done i do the following情节完成后,我会执行以下操作

plt.legend(loc=0, numpoints=1)
leg = plt.gca().get_legend()
ltext  = leg.get_texts()
plt.setp(ltext, fontsize='small') 

I don't know if this works for you我不知道这是否适合你

Now in 2021, with matplotlib 3.4.2 you can set your legend fonts with现在在 2021 年,使用 matplotlib 3.4.2,您可以设置图例字体

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.其中fontsize是图例中项目的字体大小, title_fontsize是图例标题的字体大小。 More information in matplotlib documentation matplotlib 文档中的更多信息

The most easy method is using最简单的方法是使用

title_fontsize标题字体大小

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

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