[Python] Centering a background gradient color map for a pd.DataFrame Styler object

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    Unsure if it is possible to leverage matplotlib's DivergingNorm for color maps under the framework of pandas Styler objects. As an example:

    import pandas as pd
    import matplotlib.cm

    # retrieve red-yellow-green diverging color map
    cmap = matplotlib.cm.get_cmap('RdYlGn')

    # create sample pd.DataFrame
    ix = pd.date_range(start=pd.Timestamp(2020, 1, 1), periods=4)
    df = pd.DataFrame(index=ix, columns=['D/D CHANGE'], data=[-1, 0, 2, 5])

    df.style.background_gradient(cmap=cmap)


    [​IMG]

    Ideally only negative (positive) values would appear red (green).

    Continue reading...

Compartilhe esta Página