Matplotlib子批次之间的距离

2024-04-29 12:45:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有3x3 matplotlib轴,包含sns.heatmap。在主热图ax[1][1](即2D矩形,假设形状为10x25)的左侧和上方放置了两个附加的热图(比如10x1和1x25)。在

主热图和另两个热图之间的距离取决于主热图内容的形状[0]和形状[1]的比例。。。在

我还没有找到一种方法来保持它的恒定,所以如果我试图绘制一个在维度上具有更大不均衡性的数组(比如50 x 10),其中一个距离会变得非常高:(

这幅https://ibb.co/rx9DRgp图像显示了由以下代码产生的不相等的x和y距离,这说明了这个问题。。。。在

import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import seaborn as sns

YZ = np.random.normal(loc=0.0, scale=1.0, size=(10,25))
XB = np.random.normal(loc=0.0, scale=1.0, size=(25,1))
XA = np.random.normal(loc=0.0, scale=1.0, size=(10,1))

fig, ax = plt.subplots(
    3,3,
    figsize=( YZ.shape[1], YZ.shape[0], ),
    gridspec_kw={
        "width_ratios"  : [ 1, YZ.shape[1], 0.5 ],
        "height_ratios" : [ 1, YZ.shape[0], 0.5 ],
    }
)

with sns.axes_style("darkgrid"):
    hm = {}; hm[0],hm[1],hm[2] = {},{},{}
    hm[1][1] = sns.heatmap(
        data       = YZ,
        ax         = ax[1][1],
        square     = True,
        annot_kws  = {"size": 8},
        linewidths = 0,
        cbar       = True,
        cbar_ax    = ax[1][2],
    )
    hm[1][0] = sns.heatmap(
        data       = XA,
        ax         = ax[1][0],
        square     = True,
        annot_kws  = {"size": 8},
        linewidths = 2,
        linecolor  = "white",
        cbar       = False,
    )
    hm[0][1] = sns.heatmap(
        data       = XB.T,
        ax         = ax[0][1],
        square     = True,
        annot_kws  = {"size": 8},
        linewidths = 2,
        linecolor  = "white",
        cbar       = False,
    )
ax[1][1].get_shared_x_axes().join( ax[1][1], ax[0][1] )
ax[1][1].get_shared_y_axes().join( ax[1][1], ax[1][0] )
plt.tight_layout()
plt.show()

有没有一种简单的方法可以使两个光圈的轴间距保持恒定(比如图像宽度的10%或200px)

我不知道如何使ax[1][1]和{}之间的距离等于ax[1][1]和{}之间的距离。。。在


Tags: importtrue距离sizematplotlibnppltax