matplotlib imshow以禁用的轴为中心

时间:2019-04-03 13:13:36

标签: python matplotlib imshow

在禁用轴后,如何使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中手动调整边框,但是,我希望使用一种更强大的解决方案,该解决方案可以自动处理不同的图像形状和大小。此外,在居中期间不应更改图形大小。有提示吗?

1 个答案:

答案 0 :(得分: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上正常运行(将在几天或几周内发布)。

相关问题