Imshow / Matshow在轴内收缩

时间:2019-07-10 12:55:23

标签: python matplotlib imshow

我想绘制一个数字矩阵(通过matshow / imshow完成)。然后,我想在轴边界上绘制一些与轴有关的函数。

但是:我希望matshow输出有一个黑色的边界框,但是看来我无法用edge_colorline_width来控制它。其次,这两个命令似乎都“缩小”了所产生的轴,使得轴+刻度+标签都适合轴内。但是,这使图与我要在边界上绘制的图不对齐。

特别是,请看以下图片。 matshow上方的密度图与x-axis中的matshow网格具有相同的网格,但是没有完全覆盖它。此外,它会丢失matshow周围的黑框。

如何添加/修复这两个功能?

incorrect output

这是我当前的代码段:

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
nX, nY = 50, 70
realXGrid = np.linspace(-0.5, 0.5, nX)
realYGrid = np.linspace(-1, 1, nY)
XX, YY = np.meshgrid(realXGrid, realYGrid, indexing='ij')
Z = XX * 3 - YY*5

# prepare figure/grid
fig5 = plt.figure()
widths = [1, 0.1, 0.1]
heights = [0.1, 1]
matAx = 3
caxAx = 5
spec5 = fig5.add_gridspec(ncols=3, nrows=2, width_ratios=widths,
                      height_ratios=heights, hspace=0, wspace=0)
axes = []
for row in range(2):
    for col in range(3):
        ax = fig5.add_subplot(spec5[row, col])
        axes.append(ax)

for i, ax in enumerate(axes):
    if i != matAx:
        ax.axis('off')
    else:
        ax.grid(False)
        ax.patch.set_edgecolor('black')
        ax.patch.set_linewidth('1')

# plot
cax = axes[matAx].imshow(Z, cmap='RdGy', origin='lower')
axes[matAx].axis('image')
fig5.colorbar(cax, ax=axes[caxAx], orientation='vertical', fraction=1)

xDensity = norm.pdf(realXGrid, scale=0.1)
yDensity = norm.pdf(realYGrid, scale=0.001)
axes[0].plot(realXGrid, xDensity)
axes[4].plot(yDensity, realYGrid) 
plt.show()

1 个答案:

答案 0 :(得分:1)

我建议在实际数据坐标中绘制图像。这允许对边际分布图使用相同的限制。
要获得正确的间距,您可以放弃图像的等高比。

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm

nX, nY = 50, 70
realXGrid = np.linspace(-0.5, 0.5, nX)
realYGrid = np.linspace(-1, 1, nY)
XX, YY = np.meshgrid(realXGrid, realYGrid, indexing='ij')
Z = XX * 3 - YY*5

# prepare figure/grid
fig5 = plt.figure()
widths = [1, 0.1, 0.1]
heights = [0.1, 1]
matAx = 3
caxAx = 5
spec5 = fig5.add_gridspec(ncols=3, nrows=2, width_ratios=widths,
                      height_ratios=heights, hspace=0, wspace=0)
axes = []
for row in range(2):
    for col in range(3):
        ax = fig5.add_subplot(spec5[row, col])
        axes.append(ax)

for i, ax in enumerate(axes):
    if i != matAx:
        ax.axis('off')
    else:
        ax.grid(False)
        ax.patch.set_edgecolor('black')
        ax.patch.set_linewidth('1')

# plot    
dx = np.diff(realXGrid)[0]
dy = np.diff(realYGrid)[0]
extent = [realXGrid[0]-dx/2, realXGrid[-1]+dx/2, 
          realYGrid[0]-dx/2, realYGrid[-1]+dx/2]
im = axes[matAx].imshow(Z, cmap='RdGy', origin='lower', aspect="auto",
                         extent=extent)
#axes[matAx].axis('image')
fig5.colorbar(im, ax=axes[caxAx], orientation='vertical', fraction=1)

xDensity = norm.pdf(realXGrid, scale=0.1)
yDensity = norm.pdf(realYGrid, scale=0.001)
axes[0].plot(realXGrid, xDensity)
axes[4].plot(yDensity, realYGrid) 
axes[0].set_xlim(*extent[:2])
axes[4].set_ylim(*extent[2:])
plt.show()

enter image description here