[Python] Produce a heatmap plot using seaborn with specific color mapping for values within...

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    Consider the following basic dataframe example:

    min mean max
    ALU -0.008 0.000 0.034
    BRENT -0.017 0.000 0.023
    CU -0.011 0.000 0.013
    DTD_BRENT -0.011 0.000 0.019
    GASOIL -0.009 0.000 0.035
    GOLD -0.008 0.000 0.009
    HH -0.033 -0.001 0.009
    JET_CCN -0.009 0.000 0.033


    I would like to produce a heatmap plot for the above dataframe using seaborn.heatplot where values ranging between -0.03 and 0.03 will have variations (depending on the value) of the green color and values outside this interval will have red color variations.

    While I can generally produce the plot, I am struggling with the interval-dependent color map.

    plt.figure(figsize=(12, 8))
    sns.heatmap(df, cmap=sns.color_palette(['red', 'green', 'red']), center= 0, annot=True)
    plt.title("Heatmap")
    plt.show()

    Continue reading...

Compartilhe esta Página