Search code examples
pythonpandasplotly

Colors within a 'Parcat', with Plotly


I had some difficulties giving color to a 'parcat'. My intentions were to have the last diagram displayed as different colors (for each variable a color). To make this happen I've read the particular documentation for the 'parcat'(plotly.parcat), and have followed the documentation, but I get an error which I can't fix. This is my code:

import plotly.graph_objs as go
import pandas as pd
# Create dimensions
sex_dim = go.parcats.Dimension(
    values = tips_df['sex'],
    label = "Sex",
    categoryorder = "category ascending"
)

smoker_dim = go.parcats.Dimension(
    values = tips_df['smoker'], 
    label = "Smoker")

day_dim = go.parcats.Dimension(
    values = tips_df['day'], 
    label = "Day",
    categoryorder = "category ascending"
)

time_dim = go.parcats.Dimension(
    values = tips_df['time'], 
    label = "Time",
    categoryorder = "category ascending"
)

size_dim = go.parcats.Dimension(
    values = tips_df['size'], 
    label = "Size",
    categoryorder = "category ascending"
)

quantized_total_bill = go.parcats.Dimension(
    values = tips_df['quantized_total_bill'], 
    label = "Quantized total bill",
    categoryorder = "category ascending"
)

quantized_tip = go.parcats.Dimension(
    values = tips_df['quantized_tip'], 
    label = "Quantized tip",
    categoryorder = "category ascending", 
    categoryarray=[0, 1, 2],
)

# Create parcats trace
color = tips_df.quantized_tip;
colorscale = [[0, 'lightsteelblue'], [1, 'mediumseagreen'], [2, 'yellow']];



# Create parcats trace

fig = go.Figure(data = [go.Parcats(dimensions=[sex_dim, smoker_dim, day_dim, time_dim, size_dim,
                                               quantized_total_bill, quantized_tip],
                                               line={'color': color, 'colorscale': colorscale})])

fig.update_traces(labelfont_color="black")

fig.show()

This is the error that I get:

ValueError: 
    Invalid element(s) received for the 'color' property of parcats.line
        Invalid elements include: ['Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low', 'Low']

Without specifying the colors, I get this plot, which is correct (as I followed the documentation): Plot, but when I want to add some colors (like the code above), then an error is given.

I would really appreciate it if someone could help me further, as I don't seem to figure it out, while looking at the documentation.


Solution

  • This is my first experience with this type of graph, but to customize the color specification, the range value for the color scale is 0 to 1, whereas the target column for the color is a number from 0 to 1. I use size as the color object column and divide by 10 to stay within 1. Or normalize the color target column.

    Updated: Normalized the data for the size columns used as the basis for color coding. Updated code and graphs.

    import plotly.graph_objs as go
    import plotly.express as px
    import pandas as pd
    
    tips_df = px.data.tips()
    
    # Create dimensions
    sex_dim = go.parcats.Dimension(
        values = tips_df['sex'],
        label = "Sex",
        categoryorder = "category ascending"
    )
    
    smoker_dim = go.parcats.Dimension(
        values = tips_df['smoker'], 
        label = "Smoker")
    
    day_dim = go.parcats.Dimension(
        values = tips_df['day'], 
        label = "Day",
        categoryorder = "category ascending"
    )
    
    time_dim = go.parcats.Dimension(
        values = tips_df['time'], 
        label = "Time",
        categoryorder = "category ascending"
    )
    
    size_dim = go.parcats.Dimension(
        values = tips_df['size'], 
        label = "Size",
        categoryarray=[1,2,3,4,5,6],
        categoryorder = "category ascending"
    )
    
    quantized_total_bill = go.parcats.Dimension(
        values = tips_df['total_bill'], 
        label = "Quantized total bill",
        categoryorder = "category ascending"
    )
    
    quantized_tip = go.parcats.Dimension(
        values = tips_df['tip'], 
        label = "Quantized tip",
        categoryorder = "category ascending", 
        categoryarray=[0, 1, 2],
    )
    
    # Create parcats trace
    color = [(x- tips_df['size'].min()) / (tips_df['size'].max() - tips_df['size'].min()) for x in tips_df['size']]
    colorscale = [[0.0, 'lightsteelblue'], [0.5, 'mediumseagreen'], [1.0, 'yellow']];
    
    # Create parcats trace
    fig = go.Figure(data = [go.Parcats(dimensions=[sex_dim, smoker_dim, day_dim, time_dim, size_dim],
                                                   line={'color': color, 'colorscale': colorscale})])
    
    fig.update_traces(labelfont_color="black")
    fig.show()
    

    enter image description here