I’m creating a timeline with px.timeline
, and if I don’t set the color, the y-axis shows in the correct order (as I want it):
But when I set up the colors of each of the bars, they group together and change the order:
How do I avoid that? I want the order to be the same as I give in. I have tried fig.update_yaxes(categoryorder="trace")
or adding barmode='relative'
to the layout, but none of those helped.
Thank you in advance!
Filipe
Giving this a bump as I’m also having this problem.
Have you found a solution Filipe?
Not exactly, but I found a workaround using barplots directly. I was trying to do these plots with go.Bar
, but was having problems with the dates. Then, I tried with px.timeline
and had the issue that I described above. Then, I compared the generated html outputs and found how to fix the first, by:
- adding a type:“date” to the xaxis:
fig[‘layout’][‘xaxis’].update(dict( type=“date” ))
- dividing the timedelta by 1 million (seems that the time units are the problem here):
x=(df[‘Finish’]-df[‘Start’])/1000000
So, my code looks like this:
fig.add_trace(go.Bar( x=timeline_df['duration']/1000000,
base=timeline_df['start_time'],
y=timeline_df['step'],
name = 'Timeline',
orientation='h',
hoverinfo='text',
hovertext=hovertext,
marker=dict(
color=timeline_df['colorhtml'],
line=dict(color=timeline_df['edgecolorhtml']),
),
))
I hope that helps.
I have also posted about this here.
I got that to work for me! Thanks for your help!
For reference, my code
fig_delivery = go.Figure()
fig_delivery.add_trace(
go.Bar(
x=df_delivery["days_start_to_end"],
base=df_delivery["start_num"],
y=df_delivery["id"],
orientation="h",
marker=dict(color=df_delivery["colour"]),
)
)
Great to know! I marked that as a solution now - it’s more a workaround, but it’s an easy enough way to implement.
I have the same issue. But I am not using date value but only numeric. Is there a way to make this work, and avoid grouping by color but keep the initial grouping ?
Have you tried to use the go.Bar
instead of the px.timeline
? If so, could you share a snippet of your code?
Yes, I am using bar
type, In fact am I facing the issue with R. Here’s a code snippet:
df = data.frame(
seed = c(1, 2, 3, 4, 5),
species = c("bertoniensis", "ovata", "albida", "somae", "ovata")
)
p<- plot_ly(
x = df$seed,
y = 1,
type = "bar",
color = df$species,
)
It is working fine on its own, but used within a subplot it gets grouped together by color.
I don’t know much about R, you seem to be doing as explained here:
But you can also try passing a list to marker.color
, as explained here:
I found a way to do this by adding a column ordering the datapoints and using that as an array categoryorder:
fig.update_layout(yaxis = {"categoryorder":"array", "categoryarray": df["row"]})