【问题标题】:Axis labels are cut off when using subplot with just one plot仅使用一个图的子图时,轴标签被切断
【发布时间】:2022-01-14 09:29:01
【问题描述】:

我的坐标轴标签看起来像这样

由此代码创建:

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlabel('x-label 1')
ax1.set_ylabel('y-label 1')

plt.show()

如果我将绘图的主对象(在本例中为窗口)调整为更小的尺寸,轴标签会像这样被切断

但如果我用两个图创建子图(第二个不必填充图),即使图更小,标签也不会被截断:

使用add_subplot(111)并调整其master大小时如何防止标签被切断?

【问题讨论】:

  • 使用紧凑或受限的布局:plt.figure(layout='constrained')
  • 谢谢,这行得通!想把它变成答案吗?
  • 不,它没有(调整母版大小仍然会用该方法切断标签)。

标签: python matplotlib subplot


【解决方案1】:

您应该通过设置实例来使用tightconstrained 布局

fig = plt.figure(layout='constrained')

更多详情请见Constrained Layout Guide

【讨论】:

    猜你喜欢
    • 2012-12-05
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多