matplotlib imshow 以禁用轴为中心
matplotlib imshow centering with disabled axes
如何在禁用轴后将 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 中手动调整边框,但是,我更喜欢一种更强大的解决方案,它可以自动处理不同的图像形状和大小。此外,在居中期间不应更改图形大小。有什么提示吗?
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 开始正常工作(将在几天或几周内发布)。
如何在禁用轴后将 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 中手动调整边框,但是,我更喜欢一种更强大的解决方案,它可以自动处理不同的图像形状和大小。此外,在居中期间不应更改图形大小。有什么提示吗?
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 开始正常工作(将在几天或几周内发布)。