matplotlib imshow以禁用轴为中心

问题描述 投票:0回答:1

在禁用轴后,如何将matplotlib 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个回答
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开始正常工作(将在几天或几周内发布)。

© www.soinside.com 2019 - 2024. All rights reserved.