Python:两个轴上方的标题,但不是上图

时间:2018-04-15 20:02:03

标签: python matplotlib title subplot

我正在寻找类似于plt.subtitle()的东西,这些东西会带来一个"主标题"我选择的两个子图是在网格中。在我的情节中,标题Development of model parameterFinal model parameter应该类似于两个轴的主标题。有功能吗?或者顺利解决方法?enter image description here

1 个答案:

答案 0 :(得分:1)

一种方法是为通用图创建一个不可见的轴,并仅使用该轴的标题:

import numpy as np
import matplotlib.pyplot as plt

#function for display
def f(t, a, b, c):
    return np.exp(a - t) * np.sin(b * t + c)

fig = plt.figure()

x = np.linspace(0, 10, 1000)
#curve 1
ax1 = fig.add_subplot(211)
ax1.plot(x, f(x, 5, 1, 0))
ax1.set_title("Curve 1")
#create common axis for curve 2 and 3, remove frame and ticks, set title 
ax23 = fig.add_subplot(223, frameon = False)
ax23.set_xticks([])
ax23.set_yticks([])
ax23.set_title("Curve 2 and 3 together")
#create curve 2
ax2 = fig.add_subplot(245)
ax2.plot(x, f(x, 0, 3, 0))
#create curve 3
ax3 = fig.add_subplot(246)
ax3.plot(x, f(x, 2, 1, 7))
#create curve 4
ax4 = fig.add_subplot(224)
ax4.plot(x, f(x, -1, 3, -3))
ax4.set_title("Curve 4")
#display
plt.show()

输出

enter image description here