如果我在我的 matplotlib 图中添加了一个副标题,它会被副图的标题覆盖。有人知道如何轻松解决这个问题吗?我尝试了 tight_layout()
函数,但它只会让事情变得更糟。
例子:
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
调用中调整子图几何,如下所示:
fig.tight_layout(rect=[0, 0.03, 1, 0.95])
正如文档 (https://matplotlib.org/users/tight_layout_guide.html) 中所述:
tight_layout()
only considers ticklabels, axis labels, and titles. Thus, other artists may be clipped and also may overlap.
https://stackoverflow.com/questions/8248467/