Set bar chart color and pattern based on two columns

Hi there,

i have the dataframe below. Iā€™m basically happy how its structured and displayed, but is there a way that the columns ā€œA_xā€ and ā€œA_yā€ have the same color (for instance red) and can only be distinguished by the pattern? The columns ā€œB_xā€ and ā€œB_yā€ should both be blue but ā€œB_yā€ with a pattern and so on.

Any suggestion?

import pandas as pd
import plotly.express as px

COLORS = ["red", "blue", "green", "cyan"]

df = pd.DataFrame(
            {
                "DEV" : ["L", "S", "G", "L", "S", "G", "L", "S", "G", "L", "S", "G"],
                "Par" : ["A_x", "A_x", "A_x", "A_y", "A_y", "A_y", "B_x", "B_x", "B_x", "B_y", "B_y", "B_y" ],
                "Val" : [0.14, 1.06, 1.61, 0.56, 0.84, 1.85, 1.13, 0.47, 1.33, 2.53, 0.97, 1.35],
                "Dir" : ["x", "x", "x", "y", "y", "y", "x", "x", "x", "y", "y", "y"]
            }
        )

bar_chart = px.bar(
    df, 
    x="DEV", 
    y="Val", 
    color="Par", 
    barmode='group', 
    color_discrete_sequence=COLORS, 
    text_auto=True,
    pattern_shape="Dir"
)


bar_chart.show()

If you set the color list to red and blue and the hatching to the list, you can create the following graph. Is this what you were looking for?

COLORS = ["red", "red", "blue", "blue"]
...
bar_chart = px.bar(
    df, 
    x="DEV", 
    y="Val", 
    color="Par", 
    barmode='group', 
    color_discrete_sequence=COLORS, 
    text_auto=True,
    pattern_shape="Dir",
    pattern_shape_sequence=[None, 'x'] # update
)
bar_chart.show()

Hi,

yes that solution worked! Thanks.