为图像设置标题

创建一个具有单独的子图标题和居中的图标题的图形。

为图像设置标题示例

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. def f(t):
  4. s1 = np.cos(2*np.pi*t)
  5. e1 = np.exp(-t)
  6. return s1 * e1
  7. t1 = np.arange(0.0, 5.0, 0.1)
  8. t2 = np.arange(0.0, 5.0, 0.02)
  9. t3 = np.arange(0.0, 2.0, 0.01)
  10. fig, axs = plt.subplots(2, 1, constrained_layout=True)
  11. axs[0].plot(t1, f(t1), 'o', t2, f(t2), '-')
  12. axs[0].set_title('subplot 1')
  13. axs[0].set_xlabel('distance (m)')
  14. axs[0].set_ylabel('Damped oscillation')
  15. fig.suptitle('This is a somewhat long figure title', fontsize=16)
  16. axs[1].plot(t3, np.cos(2*np.pi*t3), '--')
  17. axs[1].set_xlabel('time (s)')
  18. axs[1].set_title('subplot 2')
  19. axs[1].set_ylabel('Undamped')
  20. plt.show()

下载这个示例