【问题标题】:matplotlib imshow centering with disabled axesmatplotlib imshow 以禁用轴为中心
【发布时间】:2019-04-03 21:28:22
【问题描述】:

禁用轴后如何使 matlotlib imshow 图形居中?示例:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(5,5))
plt.axis('off')

plt.imshow(np.random.randint(10, size=(100, 100)))
plt.show()

现在图像并没有真正居中,特别是如果我应用tight_layout,因为它确实考虑了轴,尽管它们被禁用了?!

plt.tight_layout()

如果我会出现同样的问题,例如添加彩条。当然,可以通过命令或在 UI 中手动调整边框,但是,我更喜欢一种更强大的解决方案,它可以自动处理不同的图像形状和大小。此外,在居中期间不应更改图形大小。有什么提示吗?

【问题讨论】:

    标签: python matplotlib imshow


    【解决方案1】:

    default rc file设置的子图参数为

    figure.subplot.left    : 0.125  ## the left side of the subplots of the figure
    figure.subplot.right   : 0.9    ## the right side of the subplots of the figure
    figure.subplot.bottom  : 0.11   ## the bottom of the subplots of the figure
    figure.subplot.top     : 0.88   ## the top of the subplots of the figure
    

    如您所见,它们是不对称的。如果需要,您可以将它们设置为对称的东西

    rc = {"figure.subplot.left"    : 0.1, 
          "figure.subplot.right"   : 0.9,
          "figure.subplot.bottom"  : 0.1,
          "figure.subplot.top"     : 0.9 }
    plt.rcParams.update(rc)
    

    或

    fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9)
    

    fig.tight_layout() 在轴关闭时不会产生居中绘图这一事实被视为错误。此问题已得到修复,并且将从 matplotlib 3.1 开始正常工作(将在几天或几周内发布)。

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 1970-01-01
      • 2016-03-04
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 2013-09-12
      相关资源
      最近更新 更多