Search code examples
pythoncolorsplotlysankey-diagrampalette

Changing the style of Sankey/Alluvial plot with different color palettes for categorical data


I am trying to change the color of Sankey/alluvial plot using Plotly.

Fake data is available here

import plotly.express as px
fake = pd.read_csv('Fake.csv')
fig = px.parallel_categories(fake)
fig.show()

enter image description here

My ideal output - the same plot but colored with different colors based on categories. Cannot find how to apply categorical palettes here.


Solution

  • The categories you have text - RoleA/B/C/D, which are not being taken by the PX color. So, one way to do this is to add a column which will have these values converted to a numerical scale - Role A = 1; Role B = 2, etc. Once done, you can use the continuous color palette you have. Note that I have used the first column (Role 1) to do this as it has data in all columns. Hope this is what you are looking for .... and may the force be with you ;-)

    Code

    import plotly.express as px
    import pandas as pd
    fake = pd.read_csv('Fake.csv')
    
    def add_clr(row):
        if row['Role1'] == 'Role A' :
            return 1
        elif row['Role1'] == 'Role B':
            return 2
        elif row['Role1'] == 'Role C':
            return 3
        elif row['Role1'] == 'Role D':
            return 4
        else:
            return 0
            
    fake['clr']=fake.apply(add_clr, axis=1) ## New Column with numbers
    
    fig = px.parallel_categories(fake, dimensions=['Role1', 'Role2', 'Role3', 'Role4', 'Role5'],  ## Note Clr column is removed
                                 color='clr', color_continuous_scale=px.colors.sequential.Inferno) ## Use any palette u like
    fig.show()
    

    .... will give you

    enter image description here