python - matplotlib imshow 子图 sharey 打破了 x 限制

标签 python matplotlib

我正在使用 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()  

错误的输出如下:

Incorrect output with incorrect x limits

虽然简单地将 sharey=True 更改为 sharey=False 会导致正确的输出(当然我希望共享 y 轴除外,这现在不是了):

Correct output with correct x limits

有办法解决这个问题吗?

最佳答案

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() 在单个图上的行为方式与它在子图上的行为方式之间的差异。

关于python - matplotlib imshow 子图 sharey 打破了 x 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26667902/

相关文章:

python/matplotlib/seaborn- x 轴上带有数据点的箱线图

python - 具有动态属性的 Django 模型

python - python虚拟环境中plot.show()期间的UnicodeDecodeError

python - 在Python中使用字符串连接函数时如何跳过 "\n"?

python - PyQt - 加载多个 UI 文件

python - 对直方图中绘制的给定数据标准化威 bool 分布

python - 如何在运行时将 C 程序的输出连接到 python 程序

python-3.x - 如何创建两列的频率表并绘制热图

python - Pandas 识别组中第一行的列值

python - Google 数据存储区/NDB 中的重复事件