首页 > 解决方案 > 更改子图的大小,使直方图和热图 x 轴排列

问题描述

我正在尝试使用seaborn. 我能够生成具有所需格式的所有三个图形,但是,三个图形的 x 轴未对齐,如下所示。我还包含了用于生成图形的代码。

看来这是使用square=True热图选项的结果,导致它们没有使用完整的绘图空间。如何更改直方图的绘图(子图)大小,以使所有三个 x 轴对齐?有没有办法改变其中一个子图的大小?如果我必须定义每个子图的大小,有没有办法确定所有 x 轴对齐所需的大小?
示例图

style = dict(size=14, color='black')
fig, ax = plt.subplots(figsize=(12,4), ncols=3, nrows=1)
left   =  0.125  # the left side of the subplots of the figure
right  =  0.9    # the right side of the subplots of the figure
bottom =  0.1    # the bottom of the subplots of the figure
top    =  1    # the top of the subplots of the figure
wspace =  1     # the amount of width reserved for blank space between subplots
hspace =  0   # the amount of height reserved for white space between subplots

# This function actually adjusts the sub plots using the above paramters
plt.subplots_adjust(
    left    =  left, 
    bottom  =  bottom, 
    right   =  right, 
    top     =  top, 
    wspace  =  wspace, 
    hspace  =  hspace
)

boots = np.random.exponential(size=100000)
colors = sns.color_palette("colorblind")
ax[0] = sns.distplot(boots, kde=False, bins=20, ax=ax[0])
ax[0].axvline(x=0.8673, color=colors[1], linestyle = '--')
ax[0].axvline(x=.22, color="black", linestyle = "--")
ax[0].tick_params(labelsize = "13")
ax[0].set_xlabel("Average accuracy",fontsize=13)
ax[0].set_ylabel("Permutations", fontsize=13)

ax[1] = sns.heatmap(cf_matrices['CYANO-MLP'], vmin=0, vmax=1, cmap="Blues", square=True,
            linewidths=0.1, linecolor = "grey", ax=ax[1], 
                 cbar_kws={ 
                           'label': 'Mean Proportion\nClade Assignment',
                           'use_gridspec': False, 'location': "top",
                           "shrink": 1.2})
ax[1].tick_params(labelsize=13)
ax[1].text(8.2, 0.7, "N=11", **style)
ax[1].text(8.2, 1.7, "N=27", **style)
ax[1].text(8.2, 2.7, "N=30", **style)
ax[1].text(8.2, 3.7, "N=29", **style)
ax[1].text(8.2, 4.7, "N=3", **style)
ax[1].text(8.2, 5.7, "N=5", **style)
ax[1].text(8.2, 6.7, "N=4", **style)
ax[1].text(8.2, 7.7, "N=4", **style)
ax[1].figure.axes[-1].xaxis.label.set_size(13)
plt.setp(ax[1].get_xticklabels(), rotation=60)
ax[1].set_xlabel("Predicted Clade", size=15)
ax[1].set_ylabel("True Clade", size=15)

ax[2] = sns.heatmap(cf_matrices['CYANO-MLP-BAL'], vmin=0, vmax=1, cmap="Blues", square=True,
            linewidths=0.1, linecolor = "grey", ax=ax[2], 
                 cbar_kws={ 
                           'label': 'Mean Proportion\nClade Assignment',
                           'use_gridspec': False, 'location': "top",
                           "shrink": 1.2})
ax[2].tick_params(labelsize=13)
ax[2].text(8.2, 0.7, "N=30", **style)
ax[2].text(8.2, 1.7, "N=30", **style)
ax[2].text(8.2, 2.7, "N=30", **style)
ax[2].text(8.2, 3.7, "N=30", **style)
ax[2].text(8.2, 4.7, "N=30", **style)
ax[2].text(8.2, 5.7, "N=30", **style)
ax[2].text(8.2, 6.7, "N=30", **style)
ax[2].text(8.2, 7.7, "N=30", **style)
ax[2].figure.axes[-1].xaxis.label.set_size(13)
plt.setp(ax[2].get_xticklabels(), rotation=60)
ax[2].set_xlabel("Predicted Clade", size=15)
ax[2].set_ylabel("True Clade", size=15)
sns.despine(ax=ax[0])

标签: pythonmatplotlibseaborn

解决方案


推荐阅读