如何设置Matplotlib轴图例的字体大小? [英] How to set font size of Matplotlib axis Legend?

查看:113
本文介绍了如何设置Matplotlib轴图例的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

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

从图中可以看出,Fontsize中的设置不会影响图例标题字体大小.

It can be seen in the plot that the setting in Fontsize does not affect the Legend Title font size.

如何将图例标题的字体设置为较小的字体?

How to set the font size of the legend title to a smaller size?

推荐答案

这绝对是一个老问题,但也令我感到沮丧,其他任何答案都没有改变图例 title 的字体大小,但只是更改了其余的文本.因此,在将我的头撞到matplotlib文档一段时间后,我想到了这个.

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.

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

从Matplotlib 3.0.3开始,您还可以使用

As of Matplotlib 3.0.3, you can also set it globally with

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

这篇关于如何设置Matplotlib轴图例的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆