使用Seaborn绘制多热力图子图时尺寸不一致问题求助
Seaborn堆叠热力图宽度不一致问题
我是Python新手,目前使用Seaborn库绘制热力图时遇到了问题。我的目标是生成5个上下堆叠的热力图,所有热力图的列数(样本数)相同,仅行数(基因名)不同,理论上它们应该具有相同的宽度,但实际得到的热力图尺寸不一致。
问题图示:
我的代码如下:
# 仅使用数值列绘制热力图 fig, axs = plt.subplots(5, 1, figsize=(8, 20)) # 5行1列 # 调整子图间距 plt.subplots_adjust(hspace=0.1) # 为每个复合物绘制热力图 colorbar_ax = None for ax, df, show_colorbar in zip( axs, [quantity_complex_I_df, quantity_complex_II_df, quantity_complex_III_df, quantity_complex_IV_df, quantity_complex_V_df], [True, False, False, False, False]): sns.heatmap(df.select_dtypes(include=['float64', 'int64']), cmap='coolwarm', center=1, annot=False, fmt=".2f", square=True, robust=True, cbar=show_colorbar, cbar_ax=None if show_colorbar else colorbar_ax, ax=ax) # 移除x轴刻度和标签 ax.set_xticks([]) ax.set_xticklabels([]) # 调整复合物I的颜色条标签字体大小 colorbar_ax.ax.tick_params(labelsize=10) axs[0].set_ylabel('复合物I') axs[1].set_ylabel('复合物II') axs[2].set_ylabel('复合物III') axs[3].set_ylabel('复合物IV') axs[4].set_ylabel('复合物V') plt.show()
我尝试过以下方法但未解决问题:
向
subplots()传入sharex="col":fig, axs = plt.subplots(5, 1, figsize=(8, 20), sharex = "col")向
sns.heatmap()传入aspect='auto':for ax, df, show_colorbar in zip( axs, [quantity_complex_I_df, quantity_complex_II_df, quantity_complex_III_df, quantity_complex_IV_df, quantity_complex_V_df], [True, False, False, False, False]): sns.heatmap(df.select_dtypes(include=['float64', 'int64']), cmap='coolwarm', center=1, annot=False, fmt=".2f", square=False, robust=True, cbar=show_colorbar, cbar_ax=None if show_colorbar else colorbar_ax, ax=ax, aspect='auto') # 移除x轴刻度和标签 ax.set_xticks([]) ax.set_xticklabels([]) ax.set_aspect('equal')调整后热力图宽度仍不一致,且出现额外显示问题
在循环内添加
ax.set_aspect('equal'):for ax, df, show_colorbar in zip( axs, [quantity_complex_I_df, quantity_complex_II_df, quantity_complex_III_df, quantity_complex_IV_df, quantity_complex_V_df], [True, False, False, False, False]): sns.heatmap(df.select_dtypes(include=['float64', 'int64']), cmap='coolwarm', center=1, annot=False, fmt=".2f", square=True, robust=True, cbar=show_colorbar, cbar_ax=None if show_colorbar else colorbar_ax, ax=ax) ax.set_aspect('equal') # 移除x轴刻度和标签 ax.set_xticks([]) ax.set_xticklabels([])
内容的提问来源于stack exchange,提问作者Rubén Torregrosa
相关产品推荐
相关产品推荐

