[Python] How to change Labels distance from x and y ticks and choose one color to the...

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    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: [​IMG]

    Continue reading...

Compartilhe esta Página