[Python] Plotly Heatmap Exclude specific column

Discussão em 'Python' iniciado por Stack, Julho 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a pivoted dataframe, and I want to plot heatmap on plotly. Since I have a total column as well, it is making hard to use colorscale.

    import numpy as np
    import pandas as pd
    import plotly.express as px

    city_origin = ['London', 'Tokio', 'Seoul', 'Paris', 'Tashkent', 'Washington', 'Moscow']
    city_current = ['London', 'Madrid', 'Tashkent', 'Seoul', 'Paris', 'Toronto', 'Washington', 'Istanbul', 'Hanoi', 'Manilla', 'Delhi', 'Busan', 'Moscow']


    migrant_origin = np.random.choice(city_origin,size = 1000)
    migrant_current = np.random.choice(city_current, size = 1000)
    migrant_salary = np.random.randint(1300, 6900, size = 1000)
    df = pd.DataFrame({'migrant_origin':migrant_origin, 'migrant_current':migrant_current, 'migrant_salary':migrant_salary})

    new_df = pd.pivot_table(df, index = 'migrant_origin', columns = 'migrant_current', values ='migrant_salary', aggfunc = 'sum', fill_value = 0)

    new_df['total'] = new_df.sum(axis = 1)
    cols = ['total'] + [col for col in new_df.columns if col != 'total']
    new_df = new_df[cols]
    cols = ['total'] + [col for col in new_df.columns if col != 'total']
    new_df = new_df[cols]

    px.imshow(new_df,text_auto=True)


    [​IMG]

    I want to show the totals but not affected or affecting colors on the heatmap.

    Is there any way to exclude columns from coloring?

    Thank you!

    Continue reading...

Compartilhe esta Página