Your code is not working because you need to remove the external []
in x
and y
.
For a simpler way, you can just set marker_color
to a column as follows (in this case you have a single trace):
import plotly.express as px
df = px.data.gapminder().query("year == 2007").query("continent=='Europe'")
df['color'] = 'red'
df['color'][5:10] = 'black'
import plotly.graph_objects as go
fig = go.Figure(
go.Bar(x=df['country'],
y=df['gdpPercap'],
marker={'color': df['color']})
)
fig.show()
You can also use plotly.express to perform a groupby by color, and create a trace per color. This will order the bars by color
import plotly.express as px
df = px.data.gapminder().query("year == 2007").query("continent=='Europe'")
df['color'] = 'red'
df['color'][5:10] = 'black'
fig = px.bar(df, x='country', y='gdpPercap', color='color', color_discrete_sequence=df.color.unique())
fig.show()