如何更改标签距离x和y tick的距离,然后选择一种颜色到海洋热图中的注释

问题描述 投票:0回答:1
改变tick和X和Y的标签之间的距离。

也更改标题和热图之间的距离。

将注释的颜色称为白色或黑色。
    我正在使用以下代码:
  1. from matplotlib import pyplot as plt import matplotlib import numpy as np import seaborn as sns #call data frame and apply correlation: #data = #df = pd.DataFrame(data, columns = features) #df_small = df.iloc[:,:]#if only few parameters are needed #correlation_mat = df_small.corr() #Create color pallete: def NonLinCdict(steps, hexcol_array): cdict = {'red': (), 'green': (), 'blue': ()} for s, hexcol in zip(steps, hexcol_array): rgb =matplotlib.colors.hex2color(hexcol) cdict['red'] = cdict['red'] + ((s, rgb[0], rgb[0]),) cdict['green'] = cdict['green'] + ((s, rgb[1], rgb[1]),) cdict['blue'] = cdict['blue'] + ((s, rgb[2], rgb[2]),) return cdict #https://www.december.com/html/spec/colorshades.html hc = ['#e5e5ff', '#C7DDF2', '#8EBAE5', '#407FB7', '#00549F']#ffffff #e5e5ff th = [0, 0.25, 0.5, 0.75, 1] hc = hc[:0:-1] + hc # prepend a reversed copy, but without repeating the central value cdict = NonLinCdict(np.linspace(0, 1, len(hc)), hc) cm = matplotlib.colors.LinearSegmentedColormap('test', cdict) corr=np.random.uniform(-1, 1, (6,6)) #plot correlation matrix: plt.figure(figsize = (10,8)) ax=sns.heatmap(corr,center=0, linewidths=1, annot = True,cmap=cm ,square=True, vmin=-1, vmax=1, robust=True, annot_kws={'size':16}, cbar=True,linecolor='#F6A800',xticklabels=True, yticklabels=True) cbar = ax.collections[0].colorbar cbar.ax.tick_params(labelsize=10, axis='both', which='both', length=0) cbar.set_ticks(np.linspace(-1, 1, 11)) plt.title("title", y=-1.5,fontsize = 18,) plt.xlabel("X_parameters",fontsize = 18) plt.ylabel("Y_paramaters",fontsize = 18) ax.tick_params(axis='both', which='both', length=0) ax.axhline(y=0, color='#F6A800',linewidth=4) ax.axhline(y=corr.shape[1], color='#F6A800',linewidth=4) ax.axvline(x=0, color='#F6A800',linewidth=4) ax.axvline(x=corr.shape[0], color='#F6A800',linewidth=4) #change position of lables and titles and assign colors. plt.show()
  2. 我的当前输出是:

Well,

plt.title()

具有一个参数Outputpad=

来在标题的文本和图的顶部脊柱之间设置填充(默认为6)。
python annotations seaborn heatmap
1个回答
1
投票
plt.xlabel()

plt.ylabel()
具有一个参数labelpad=
来设置轴标签和ticklabels之间的距离。
sns.heatmap()
具有一个参数
annot_kws
,它是注释文本的参数字典。 可以通过
sns.heatmap(..., annot_kws={'size': 16, 'color': 'black'})
更改颜色。请注意,对于可读性,Seaborn的默认值将白色用于深色细胞上的文本,而黑色则用于浅色单元格。

from matplotlib import pyplot as plt import matplotlib import numpy as np import seaborn as sns def NonLinCdict(steps, hexcol_array): cdict = {'red': (), 'green': (), 'blue': ()} for s, hexcol in zip(steps, hexcol_array): rgb = matplotlib.colors.hex2color(hexcol) cdict['red'] = cdict['red'] + ((s, rgb[0], rgb[0]),) cdict['green'] = cdict['green'] + ((s, rgb[1], rgb[1]),) cdict['blue'] = cdict['blue'] + ((s, rgb[2], rgb[2]),) return cdict hc = ['#e5e5ff', '#C7DDF2', '#8EBAE5', '#407FB7', '#00549F'] # ffffff #e5e5ff th = [0, 0.25, 0.5, 0.75, 1] hc = hc[:0:-1] + hc # prepend a reversed copy, but without repeating the central value cdict = NonLinCdict(np.linspace(0, 1, len(hc)), hc) cm = matplotlib.colors.LinearSegmentedColormap('test', cdict) corr = np.random.uniform(-1, 1, (6, 6)) # plot correlation matrix: plt.figure(figsize=(10, 8)) ax = sns.heatmap(corr, center=0, linewidths=1, annot=True, cmap=cm, square=True, vmin=-1, vmax=1, robust=True, annot_kws={'size': 16, 'color': 'black'}, cbar=True, linecolor='#F6A800', xticklabels=True, yticklabels=True) cbar = ax.collections[0].colorbar cbar.ax.tick_params(labelsize=10, axis='both', which='both', length=0) cbar.set_ticks(np.linspace(-1, 1, 11)) plt.title("title", y=-1.5, fontsize=18, pad=15) plt.xlabel("X_parameters", fontsize=18, labelpad=15) plt.ylabel("Y_paramaters", fontsize=18, labelpad=15) ax.tick_params(axis='both', which='both', length=0) ax.axhline(y=0, color='#F6A800', linewidth=4) ax.axhline(y=corr.shape[1], color='#F6A800', linewidth=4) ax.axvline(x=0, color='#F6A800', linewidth=4) ax.axvline(x=corr.shape[0], color='#F6A800', linewidth=4) plt.show()

    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.