Matplotlibight_layout()未考虑人物字幕 [英] Matplotlib tight_layout() doesn't take into account figure suptitle

查看:56
本文介绍了Matplotlibight_layout()未考虑人物字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在matplotlib图上添加了字幕,则该字幕会被该子图的标题覆盖.有人知道如何轻松地解决这个问题吗?我尝试了tight_layout()函数,但这只会使情况变得更糟.

If I add a subtitle to my matplotlib figure it gets overlaid by the subplot's titles. Does anybody know how to easily take care of that? I tried the tight_layout() function, but it only makes things worse.

示例:

import numpy as np
import matplotlib.pyplot as plt

f = np.random.random(100)
g = np.random.random(100)
fig = plt.figure()
fig.suptitle('Long Suptitle', fontsize=24)
plt.subplot(121)
plt.plot(f)
plt.title('Very Long Title 1', fontsize=20)
plt.subplot(122)
plt.plot(g)
plt.title('Very Long Title 2', fontsize=20)
plt.tight_layout()
plt.show()

推荐答案

您可以在tight_layout调用中如下调整子图几何:

You can adjust the subplot geometry in the very tight_layout call as follows:

fig.tight_layout(rect=[0, 0.03, 1, 0.95])

如文档中所述( https://matplotlib.org/users/tight_layout_guide.html):

tight_layout()仅考虑刻度标签,轴标签和标题.因此,其他艺术家可能会被剪裁,也可能会重叠.

tight_layout() only considers ticklabels, axis labels, and titles. Thus, other artists may be clipped and also may overlap.

这篇关于Matplotlibight_layout()未考虑人物字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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