相关性热图绘制及配色方案(更新)(Python)

 微生物属与环境因子之间的相关性绘图,Spearman计算方法

数据结构:土壤理化性质和菌属之间有一一对应的site标号

得到的结果(部分展示)

配色方案(RGB以及对应的16进制)代码里用的是RGB

  • RGB: (198, 91, 63) Hex: #C65B3F
  • RGB: (222, 160, 147) Hex: #DEA093
  • RGB: (237, 204, 197) Hex: #EDCCC5
  • RGB: (215, 230, 234) Hex: #D7E6EA
  • RGB: (119, 165, 179) Hex: #77A5B3
  • RGB: (64, 128, 148) Hex: #408094 ​
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from scipy.stats import spearmanr

# Load the Excel files
genusbasic_df = pd.read_excel('path_to/Genusbasic.xlsx')
sampleinformation_df = pd.read_excel('path_to/Sampleinformation.xlsx')

# Merge the two dataframes on 'site' column
merged_df = pd.merge(genusbasic_df, sampleinformation_df, on='site')

# Drop the 'site' column as it is not needed for correlation analysis
merged_df = merged_df.drop(columns=['site'])

# Calculate the correlation matrix
correlation_matrix = merged_df.corr()

# Separate the correlation matrix into two parts: one for Genus vs soil properties
genus_columns = genusbasic_df.columns[1:]
soil_properties_columns = sampleinformation_df.columns[1:]

genus_soil_corr = correlation_matrix.loc[soil_properties_columns, genus_columns]

# Define the inverted custom colormap
inverted_cmap = mcolors.LinearSegmentedColormap.from_list("inverted_cmap", 
                                                          [(64/255, 128/255, 148/255),
                                                           (119/255, 165/255, 179/255),
                                                           (215/255, 230/255, 234/255),
                                                           (237/255, 204/255, 197/255),
                                                           (222/255, 160/255, 145/255),
                                                           (198/255, 91/255, 63/255)])

# Plot the heatmap with the inverted colormap
plt.figure(figsize=(14, 10))

# Create the heatmap with the inverted custom colormap and circular cells
heatmap = sns.heatmap(genus_soil_corr, annot=True, cmap=inverted_cmap, vmin=-1, vmax=1, center=0, linewidths=.5, linecolor='grey', cbar_kws={'ticks': [-0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8]})

# Calculate p-values and add significance markers in a position to avoid covering values
for i in range(len(soil_properties_columns)):
    for j in range(len(genus_columns)):
        _, p_value = spearmanr(merged_df[soil_properties_columns[i]], merged_df[genus_columns[j]])
        if p_value < 0.05:
            if p_value < 0.01:
                heatmap.text(j + 0.9, i + 0.7, '**', ha='center', va='center', color='black', fontsize=12)
            else:
                heatmap.text(j + 0.9, i + 0.7, '*', ha='center', va='center', color='black', fontsize=12)

# Adjust x-axis labels to 45 degrees
plt.xticks(rotation=45, ha='right')

# Adjust the plot to leave a small margin between axes and the plot
plt.title('Spearman Correlation Heatmap')
plt.xlabel('Genus')
plt.ylabel('Soil Properties')
plt.tight_layout(pad=2.0)
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值