✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Plotly express color bar argument missing go.Bar

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")

If I want to write the same code in plotly.graph_objects , than how I can include the color argument without using for loop. Basically I want to use bar and line chart in same axis with bar having a color code based on a different variable and pass that as a return in Dash.

bars = []
for label, label_df in df.groupby('label'):
    bars.append(go.Bar(x=label_df.x,  y=label_df.y,    name=label,      marker={'color': colors[label]}))
trace1 = bars
# trace2 = go.Scatter(x=label_df.x, y=label_df.y, name='Nifty 50', mode='lines+markers')

fig = go.Figure(data=[trace1])


Hi @krisvija! You have to do the for loop if your colors are discrete colors, ie if you want one trace per color. If you are fine with having your color to be a continuous variable then you can transform your categorical labels to codes using pandas method for this

import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['sepal_width'], y=df['petal_width'],

Note that this will not work for lines. Plotly express was introduced exactly to address this kind of situations in a compact and easy way so it would be better if you can use plotly express.

For more information on colors, see the tutorial on discrete colors and the one on continuous colors.