I have a DataFrame containing multiple features along with their associated t-test results and p-values. I aim to generate a combined heatmap in Python using Seaborn. In this heatmap, one section should display the features with normalized data using z-scores (to ensure visibility of both high and low values), while the other section should present the original t-test values and p-values.
I intend to create a single heatmap with distinct color schemes for each section to clearly differentiate between them. However, my attempts to plot two separate heatmaps and combine them have resulted in separate plots rather than a unified heatmap.
Could someone guide me on how to create a single combined heatmap where both sections appear attached?
Here's the code I've attempted so far:import pandas as pdimport numpy as npimport seaborn as snsimport matplotlib.pyplot as pltfrom matplotlib import gridspec
# Example DataFramedata = {'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 columnsdf_heatmap = df.iloc[:, :-2]# Calculate z-scores for the DataFramedf_heatmap_zscore = (df_heatmap - df_heatmap.mean()) / df_heatmap.std()# Set up the layoutfig = 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 columnsax1 = 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-valuesax2 = 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-scorecbar_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-valuescbar_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()
Is there a way to combine these heatmaps into a single plot, so they appear attached and have different color pattern and legend bar?