我正在使用 matplotlib 绘制一系列热图。没有共享 y 轴,它工作正常。但是,当我尝试共享 y 轴时遇到了问题。 x 轴限制似乎被破坏了。
考虑以下 MWE:
import matplotlib
print matplotlib.__version__ # prints "1.4.2"
import matplotlib.pyplot as plt
data = [[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]]
nrows, ncols = 1, 4
fig, axes = plt.subplots(nrows, ncols, sharey=True)
for j in range(ncols):
xs = axes[j]
# seems to have no impact when sharey=True
#xs.set_xlim(-0.5, 2.5)
xs.imshow(data, interpolation='none')
plt.show()
不正确的输出如下所示:
而简单地将
sharey=True
更改为 sharey=False
会产生正确的输出(当然,我希望共享 y 轴,但现在不是):有没有办法解决这个问题?
最佳答案
从 here 得到答案:
ax.set_adjustable('box-forced')
所以:
for j in range(ncols):
xs = axes[j]
xs.set_adjustable('box-forced')
xs.imshow(data, interpolation='none')
似乎这是故意的行为,您需要指定它以协调 imshow() 在单个绘图上的行为方式与它在 sublplot 上的其他行为方式之间的差异。
关于python - matplotlib imshow subplots sharey 打破 x 限制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26667902/