I'd like add conditional styling to a plotly stacked bar plot. Specifically, the top bar of the stack would be conditional based on the x-axis values.
Here's my code:
# sample df
df = pd.DataFrame({'Date': ['2010 - Q3','2010 - Q4','2011 - Q1','2011 - Q2','2011 - Q3','2011 - Q4'],
'Rate' : ['11.4','12.2','14.4','15.5','10.1','13.1'],
'Rate1': ['2.1','2.3','1.9','1.6','2.5','1.1']
})
clrs = 'rgb(222,0,0)'
fig = go.Figure(
data=[
go.Bar(
x=df['Date'],
y=df['Rate'],
name='Natural Level'
),
go.Bar(
x=df['Date'],
y=df['Rate1']),
name='Change',
marker=dict(color=clrs)
)
],
layout=go.Layout(
title='Measuring excess demand and supply in the market.',
xaxis=dict(
tickangle=90,
tickfont=dict(family='Rockwell', color='crimson', size=14)
),
yaxis=dict(
title='Rate',
showticklabels=True
),
barmode='stack',
)
)
The clrs variable takes the color value conditional on the list of x axis values. i.e clrs = rgb(222,0,0) if df['Date'] in lst else rgb(0,222,0).
where lst is list of x-axis values.


