pythonannotationsseabornheatmap

How to change Labels distance from x and y ticks and choose one color to the annotation in seaborn heat map


I am doing a correlation matrix using a seaborn heatmap, I need to :

  1. Change the distance between the ticks and the labels of x and y.
  2. Also, change the distance between the title and the heatmap.
  3. Unifying the color of annotation to be either white or black.

I am using the following code:

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()

My current output is: Output


Solution

  • Well, plt.title() has a parameter pad= to set the padding between the text of the title and the top spine of the plot (default is 6). plt.xlabel() and plt.ylabel() have a parameter labelpad= to set the distance between the axis label and the ticklabels.

    sns.heatmap() has a parameter annot_kws which is a dictionary of parameters for the annotation texts. The color can be changed via sns.heatmap(..., annot_kws={'size': 16, 'color': 'black'}). Note that for readability, seaborn's default uses white for text on the dark-colored cells, and black for the light-colored cells.

    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()
    

    resulting plot