Trying to assign patterns to a bar graph using a dataframe columns

I am trying to assign patterns to a stacked bar graph using a dataframe columns using graph_objects but keep getting an error. Here is the code:

fig = go.Figure()

fig.add_traces(go.Bar(x=ver_1['LapNumber'],y=['Verstappen'],orientation='h',marker_color='#ffd12e',name='M',text=ver_1['LapNumber'],pattern_shape=ver_1['FreshTyre']))
fig.add_traces(go.Bar(x=ver_2['LapNumber'],y=['Verstappen'],orientation='h',marker_color='#43b02a',name='I',text=ver_2['LapNumber'],pattern_shape=ver_2['FreshTyre']))
fig.add_traces(go.Bar(x=ver_3['LapNumber'],y=['Verstappen'],orientation='h',marker_color='#f0f0ec',name='H',text=ver_3['LapNumber'],pattern_shape=ver_3['FreshTyre']))

Hi @MJF ,

I think you need to replace y attribute from

y=['Verstappen']

to

y=ver_1['Verstappen']

and also for other traces as well.

Because you use graph objects , not Plotly Express, you can add your shape use marker_pattern_shape instead of pattern_shape.
The available shape is one of [“”,“/”, “'”, “x”, “-”, “|”, “+”, “.”].

for example.

marker_pattern_shape="+"

At the bottom line you need to put

# Change the bar mode
fig.update_layout(barmode='stack')

Hope this help.

Thanks for your answer. I am trying to assign pattern to the bar graph based on the value of a column. For example, if that column has a ‘No’ value, we assign a pattern to that bar graph. You can do that in plotly express but I think its not supported in graph objects.

Hi @MJF ,
Yes, you need to set the type of pattern shape in graph objects, not the column name or column value.

But, if you want to use plotly express, try code below.

import plotly.express as px

fig = px.bar(ver_1, x="LapNumber", y="Verstappen",orientation='h',color='LapNumber',pattern_shape="FreshTyre")
fig1 = px.bar(ver_2, x="LapNumber", y="Verstappen",orientation='h',color='LapNumber',pattern_shape="FreshTyre")
fig2 = px.bar(ver_3, x="LapNumber", y="Verstappen",orientation='h',color='LapNumber',pattern_shape="FreshTyre")

fig.add_traces(fig1.data)
fig.add_traces(fig2.data)
fig.update_layout(barmode='stack')
fig.show()

Thank you very much