绘制变量热力图,并且给显著变量增加*号,灵活调整seaborn绘制不能更改color位置的bug

import os
import seaborn as sns
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.gridspec import GridSpec
import matplotlib.gridspec as gridspec
pathSep = os.path.sep

df = pd.read_excel('r-value.xlsx', sheet_name='TOT', header=None,  index_col=None)

plt.rcParams['mathtext.default']='regular'   #数学格式
plt.rc('font', family='serif')
plt.rc('font', serif='Times New Roman')
#plt.rcParams['font.serif'] = 'Times New Roman'
mpl.rcParams['xtick.labelsize'] = 13
mpl.rcParams['ytick.labelsize'] = 15
#mpl.rcParams['axes.titlesize'] = 20
mpl.rcParams['axes.labelsize'] = 15

fig, ax1 = plt.subplots(figsize=(5,8), nrows=1)

# for i in [0.75, 1.75, 2.75, 3.75, 4.75]:
#     for j in [0.45, 1.45, 2.45, 3.45, 4.45]:
#         plt.text(i, j, "**", size = 10, alpha = 2, color = "black")

# sns.heatmap(data=df, linecolor='white', annot=True, fmt='.1f',annot_kws={'size':15}, ax = ax1, robust=True, vmin=-25, vmax=0, center=-12.5,
#                cmap=cmap, cbar=False)#,cbar_kws={'label':r'$\mathregular{N_i}{_n}$ change (%)',"shrink": 0.72})   square=1 'weight':'bold'

s = sns.heatmap(df, ax = ax1,     #此处为相关系数矩阵
                annot=True, #annot设置为True,使得heatmap中每个方格显示相关系数
                fmt= '.2f',  #设置相关系数保留三位小数
                annot_kws={'size':13}, #设置相关系数保显示大小
                vmax=0.5,vmin=-0.5,  #设置图例中最大值和最小值的显示值
                cmap='RdBu',  #此处为matplotlib的colormap名称或颜色对象
                square=1, cbar=False)

ax1.set_yticklabels(['aPr', '$\mathregular{N_{in}}$', 'BD', '$\mathregular{diffPr}$', 'gsPr', 'ferPr', 'aT', 'gsT', 'yield', 'ferPr', 'aT', 'gsT'], rotation=0)
ax1.set_xticklabels(['Maize IRR', 'Maize RFD', 'Rice IRR', 'Rice RFD', 'Wheat IRR', 'Wheat RFD'], rotation=30)
ax1.tick_params(axis='x', which='both', pad=23)
ax1.text(0.36, -0.04, "Extreme $\mathregular{N_w}$",transform=ax1.transAxes, fontsize=16)
# ax1.xaxis.set_label_position("top")
xx_pvalue =  [[0,0], [0,1], [0,4], [0,5],
              [1,1], [1,5],
              [2,2], [2,3], [2,5],
              [3,0], [3,1], [3,2], [3,4], [3,5],
              [4,1], [4,4], [4,5],
              [7,3],
              [8,1], [8,4], [8,5],
              [9,5],
              [10,0], [10,1]]

widthx = 0.5
widthy = 0.37

for m in ax1.get_yticks():
    m = m - 0.5
    for n in ax1.get_xticks():
        n = n - 0.5
        grid = [m, n]
        if grid in xx_pvalue:
            ax1.text(n+widthx,m+widthy, '*', size = 16, ha = 'center',color = 'black')

#这里可以灵活调整colorbar的位置
rect = [0.90, 0.075, 0.03, 0.85]  #左边缘, 底边缘, 宽度, 高度
cbar_ax = fig.add_axes(rect)
cbar = ax1.collections[0]
cb = fig.colorbar(cbar, cax=cbar_ax, extend='both')
cb.ax.tick_params(labelsize=15, size=4)
cb.set_label(r'Pearson Correlation Coefficient (r)') 

# labels = ax1.get_xticklabels() + ax1.get_yticklabels()
# [label.set_fontweight('bold') for label in labels]

plt.subplots_adjust(top=0.94, bottom=0.06,left=0.08, right=0.95,hspace=0.12)
plt.savefig('figure4_4-27.tif',format='TIFF', dpi=600, bbox_inches='tight')

你可能感兴趣的:(python,matplotlib,开发语言)