Discrete color sequence for violin plot

How can I apply the color sequence color_discrete_sequence=["green", "blue", "yellow", "magenta"] to the days “Sunday”, “Saturday”, “Thursday” “Friday” in the plot below?

import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

fig = go.Figure()

fig.add_trace(go.Violin(x=df['day'], y=df['total_bill'],                        
                        line_color='rgba(255,0,0,0.5)'
                       )
             )


fig.update_traces(box_visible=False, meanline_visible=True,
                  points='all', pointpos=-0, jitter=0.5,
                  marker_line_color='rgba(0,0,0,0.5)',
                  marker_line_width=1,                  
                  showlegend=False)

fig.update_layout(template='simple_white')

fig.show()

gives

one of the many ways

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

dict_color = {
    'Sun':"green",
    'Sat':"blue",
    'Thur':"yellow",
    'Fri':"magenta"
}

fig = go.Figure()

for dow in df.day.unique():
    
    df_temp = df.loc[df.day==dow, :]
    
    fig.add_trace(go.Violin(
        x=df_temp['day'], 
        y=df_temp['total_bill'],
        line_color=dict_color[dow],
    ))

fig.update_traces(box_visible=False, meanline_visible=True,
                  points='all', pointpos=-0, jitter=0.5,
                  marker_line_color='rgba(0,0,0,0.5)',
                  marker_line_width=1,                  
                  showlegend=False)

fig.show()
1 Like

Thanks, I figured that too by now :slight_smile:

1 Like