使用紧凑布局调整轴的大小

tight_layout 尝试调整图中子图的大小,以便轴对象和轴上的标签之间不会重叠。

有关详细信息,请参阅 “约束布局指南”;有关替代方法,请参阅 “严格布局” 指南。

  1. import matplotlib.pyplot as plt
  2. import itertools
  3. import warnings
  4. fontsizes = itertools.cycle([8, 16, 24, 32])
  5. def example_plot(ax):
  6. ax.plot([1, 2])
  7. ax.set_xlabel('x-label', fontsize=next(fontsizes))
  8. ax.set_ylabel('y-label', fontsize=next(fontsizes))
  9. ax.set_title('Title', fontsize=next(fontsizes))
  1. fig, ax = plt.subplots()
  2. example_plot(ax)
  3. plt.tight_layout()

紧凑布局示例

  1. fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
  2. example_plot(ax1)
  3. example_plot(ax2)
  4. example_plot(ax3)
  5. example_plot(ax4)
  6. plt.tight_layout()

紧凑布局示例2

  1. fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
  2. example_plot(ax1)
  3. example_plot(ax2)
  4. plt.tight_layout()

紧凑布局示例3

  1. fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
  2. example_plot(ax1)
  3. example_plot(ax2)
  4. plt.tight_layout()

紧凑布局示例4

  1. fig, axes = plt.subplots(nrows=3, ncols=3)
  2. for row in axes:
  3. for ax in row:
  4. example_plot(ax)
  5. plt.tight_layout()

紧凑布局示例5

  1. fig = plt.figure()
  2. ax1 = plt.subplot(221)
  3. ax2 = plt.subplot(223)
  4. ax3 = plt.subplot(122)
  5. example_plot(ax1)
  6. example_plot(ax2)
  7. example_plot(ax3)
  8. plt.tight_layout()

紧凑布局示例6

  1. fig = plt.figure()
  2. ax1 = plt.subplot2grid((3, 3), (0, 0))
  3. ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
  4. ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
  5. ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
  6. example_plot(ax1)
  7. example_plot(ax2)
  8. example_plot(ax3)
  9. example_plot(ax4)
  10. plt.tight_layout()
  11. plt.show()

紧凑布局示例7

  1. fig = plt.figure()
  2. import matplotlib.gridspec as gridspec
  3. gs1 = gridspec.GridSpec(3, 1)
  4. ax1 = fig.add_subplot(gs1[0])
  5. ax2 = fig.add_subplot(gs1[1])
  6. ax3 = fig.add_subplot(gs1[2])
  7. example_plot(ax1)
  8. example_plot(ax2)
  9. example_plot(ax3)
  10. with warnings.catch_warnings():
  11. warnings.simplefilter("ignore", UserWarning)
  12. # This raises warnings since tight layout cannot
  13. # handle gridspec automatically. We are going to
  14. # do that manually so we can filter the warning.
  15. gs1.tight_layout(fig, rect=[None, None, 0.45, None])
  16. gs2 = gridspec.GridSpec(2, 1)
  17. ax4 = fig.add_subplot(gs2[0])
  18. ax5 = fig.add_subplot(gs2[1])
  19. example_plot(ax4)
  20. example_plot(ax5)
  21. with warnings.catch_warnings():
  22. # This raises warnings since tight layout cannot
  23. # handle gridspec automatically. We are going to
  24. # do that manually so we can filter the warning.
  25. warnings.simplefilter("ignore", UserWarning)
  26. gs2.tight_layout(fig, rect=[0.45, None, None, None])
  27. # now match the top and bottom of two gridspecs.
  28. top = min(gs1.top, gs2.top)
  29. bottom = max(gs1.bottom, gs2.bottom)
  30. gs1.update(top=top, bottom=bottom)
  31. gs2.update(top=top, bottom=bottom)
  32. plt.show()

紧凑布局示例8

参考

此示例中显示了以下函数和方法的用法:

  1. import matplotlib
  2. matplotlib.pyplot.tight_layout
  3. matplotlib.figure.Figure.tight_layout
  4. matplotlib.figure.Figure.add_subplot
  5. matplotlib.pyplot.subplot2grid
  6. matplotlib.gridspec.GridSpec

脚本的总运行时间:(0分1.072秒)

下载这个示例