本文介绍了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):
这篇关于Matplotlibight_layout()未考虑人物字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!