对齐标签

使用 Figure.align_xlabelsFigure.align_ylabels 对齐xlabel和ylabel

Figure.align_labels 包装了这两个函数。

注意,xlabel “XLabel11” 通常更接近x轴,“YLabel1 0” 将更接近其各自轴的y轴。

对齐标签图示

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import matplotlib.gridspec as gridspec
  4. fig = plt.figure(tight_layout=True)
  5. gs = gridspec.GridSpec(2, 2)
  6. ax = fig.add_subplot(gs[0, :])
  7. ax.plot(np.arange(0, 1e6, 1000))
  8. ax.set_ylabel('YLabel0')
  9. ax.set_xlabel('XLabel0')
  10. for i in range(2):
  11. ax = fig.add_subplot(gs[1, i])
  12. ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))
  13. ax.set_ylabel('YLabel1 %d' % i)
  14. ax.set_xlabel('XLabel1 %d' % i)
  15. if i == 0:
  16. for tick in ax.get_xticklabels():
  17. tick.set_rotation(55)
  18. fig.align_labels() # same as fig.align_xlabels(); fig.align_ylabels()
  19. plt.show()

下载这个示例