我有一个DataFrame,包含多个功能及其相关的t测试结果和p值.我的目标是使用Seaborn在Python中生成组合热图.在此热图中,一个部分应该使用z得分显示具有标准化数据的特征(以确保高值和低值的可见性),而另一个部分应该呈现原始的t测试值和p值.
我打算 for each 部分创建一个具有不同配色方案的单一热图,以清楚地区分它们.然而,我试图绘制两个单独的热图并将它们组合起来,结果是单独的图而不是统一的热图.
有人可以指导我如何创建一个单一的组合热图,其中两个部分都显示为附加的?
以下是迄今为止我try 过的代码: 导入Pandas 作为PD 将numpy导入为NP 进口海运为sns 将matplotlib.pyplot导入为plt 从matplotlib导入gridspec
# Example DataFrame
data = {
'Feature1': np.random.randn(10),
'Feature2': np.random.randn(10),
'Feature3': np.random.randn(10),
't test': np.random.randn(10),
'p value': np.random.rand(10)
}
df = pd.DataFrame(data)
# Drop the last two columns
df_heatmap = df.iloc[:, :-2]
# Calculate z-scores for the DataFrame
df_heatmap_zscore = (df_heatmap - df_heatmap.mean()) / df_heatmap.std()
# Set up the layout
fig = plt.figure(figsize=(12, 8))
gs = gridspec.GridSpec(1, 4, width_ratios=[1, 1, 0.05, 0.05]) # 4 columns: 2 for heatmaps, 2 for colorbars
# Heatmap for the DataFrame excluding t-test and p-value columns
ax1 = plt.subplot(gs[0])
sns.heatmap(df_heatmap_zscore, cmap='coolwarm', annot=True, cbar=False)
plt.title('Heatmap without t-test and p-value')
# Heatmap for t-test p-values
ax2 = plt.subplot(gs[1])
sns.heatmap(df[['t test', 'p value']], cmap='viridis', annot=True, fmt=".4f", cbar=False, ax=ax2)
plt.title('Heatmap for t-test p-values')
# Create a single colorbar for the z-score
cbar_ax1 = plt.subplot(gs[2])
cbar1 = plt.colorbar(ax1.collections[0], cax=cbar_ax1, orientation='vertical')
cbar1.set_label('Z-score')
# Create a single colorbar for the t-test p-values
cbar_ax2 = plt.subplot(gs[3])
cbar2 = plt.colorbar(ax2.collections[0], cax=cbar_ax2, orientation='vertical')
cbar2.set_label('p-value')
plt.tight_layout()
plt.show()
有没有方法将这些热图组合到一个单独的情节中,使它们看起来是附加的并且具有不同的 colored颜色 图案和说明栏?