我有一个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()

enter image description here

有没有方法将这些热图组合到一个单独的情节中,使它们看起来是附加的并且具有不同的 colored颜色 图案和说明栏?

推荐答案

对于下图,我使用fig.add_axes(而不是gridspec)按顺序添加每个轴,因为我更熟悉该方法.

enter image description here

from matplotlib import pyplot as plt
import seaborn as sns

import pandas as pd
import numpy as np

# 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, 5))
heatmap1_width = 0.15
heatmap2_width = 0.1
cbar1_left_gap = 0.015
cbar2_left_gap = 0.06
cbar_width = 0.02
start_x = 0

heatmap_kwargs = dict(annot=True, cbar=False, annot_kws={'size': 9})

# Heatmap for the DataFrame excluding t-test and p-value columns
ax1 = fig.add_axes([start_x, 0, heatmap1_width, 1])
sns.heatmap(df_heatmap_zscore, cmap='coolwarm', ax=ax1, **heatmap_kwargs)
ax1.set_title('Heatmap without\nt-test and p-value', fontsize=10)
start_x += heatmap1_width

# Heatmap for t-test p-values
ax2 = fig.add_axes([start_x, 0, heatmap2_width, 1])
sns.heatmap(df[['t test', 'p value']], cmap='viridis', fmt=".4f", ax=ax2, **heatmap_kwargs)
ax2.tick_params(axis='y', left=False, labelleft=False) #no ticks on ax2
ax2.set_title('Heatmap for\nt-test p-values', fontsize=10)
start_x += heatmap2_width

#rotate if desired & remove tick marks
[ax.tick_params(axis='x', rotation=45, bottom=False) for ax in [ax1, ax2]]

#Colorbar 1
cax1 = fig.add_axes([start_x + cbar1_left_gap, 0, cbar_width, 1])
cbar1 = plt.colorbar(ax1.collections[0], cax=cax1, orientation='vertical')
cbar1.set_label('Z-score', size=9)
start_x += cbar1_left_gap + cbar_width

#Colorbar 2
cax2 = fig.add_axes([start_x + cbar2_left_gap, 0, cbar_width, 1])
cbar2 = plt.colorbar(ax2.collections[0], cax=cax2, orientation='vertical')
cbar2.set_label('p-value', size=9)

Python相关问答推荐

通过仅导入pandas来在for循环中进行多情节

将DF中的名称与另一DF拆分并匹配并返回匹配的公司

Python多处理:当我在一个巨大的pandas数据框架上启动许多进程时,程序就会陷入困境

try 与gemini-pro进行多轮聊天时出错

使用numpy提取数据块

根据在同一数据框中的查找向数据框添加值

如何避免Chained when/then分配中的Mypy不兼容类型警告?

如何使用根据其他值相似的列从列表中获取的中间值填充空NaN数据

Telethon加入私有频道

修复mypy错误-赋值中的类型不兼容(表达式具有类型xxx,变量具有类型yyy)

如何获取numpy数组的特定索引值?

如何合并两个列表,并获得每个索引值最高的列表名称?

Python中的变量每次增加超过1

在www.example.com中使用`package_data`包含不包含__init__. py的非Python文件

Python Pandas获取层次路径直到顶层管理

在嵌套span下的span中擦除信息

不允许 Select 北极滚动?

如何强制向量中的特定元素在Gekko中处于优化解决方案中

一个telegram 机器人应该发送一个测验如何做?""

将数字数组添加到Pandas DataFrame的单元格依赖于初始化