✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Bar Charts With Multicategory Axis Type X-Axis Ordering Bug?

Hello,

I am coming across the most bizzare bug with using “Bar Charts With Multicategory Axis Type”. Plotly forces a re-ordering of the second x-axis instead of following the order of values I give to the x axis variable.

The example in Plotly docs works fine.

However, when I change the x-axis to year and month numbers for financial reporting, Plotly will re-order the second x-axis (months) into a new order instead of respecting the given order (i.e. 1,2,3,…,10,11,12). See code below. I explicitly give Plotly the order I want to see the x-axis items, but it still re-arranges the months 11, 12. Anybody know how to fix ordering issue?

import plotly.graph_objects as go

fig = go.Figure()

## Plotly example works with text
# x = [['Second', 'Second', 'First', 'First'], ["B", "A", "B", "A"]]

## Plotly respects number order as long as they repeat
# x = [['Second', 'Second', 'First', 'First'], [2, 1, 2, 1]]

## Plotly does not follow the order of months (see months 11, 12 for 2011)
x = [[2010,2010, 2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011],
     [11,12, 1,2,3,4,5,6,7,8,9,10,11,12]]


fig.add_trace(go.Bar(
  x = x,
  y = [64, 96, 17, 42, 85, 94, 79, 27, 96, 58, 76, 52, 73, 11],
  name = "Adults",
))

fig.add_trace(go.Bar(
  x = x,
  y = [42, 33, 63, 42, 17, 98, 86, 24, 75, 20, 37, 40, 91, 92], 
  name = "Children",
))

fig.show()

To add another issue to the same overall problem, the UI feature that shows ‘data on hower’ is not showing the correct values for the bar charts if they are set to stack above/below the x-axis. See the example below where the ‘expense’ trace is not showing the correct values when hovering over it. I have added a ‘text’ field to show the correct values, but I don’t know how to fix the default values being shown?

import plotly.graph_objects as go

fig = go.Figure()

## Plotly does not follow the order of months (see months 11, 12 for 2011)
x = [[2010,2010, 2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011],
     [11,12, 1,2,3,4,5,6,7,8,9,10,11,12]]


fig.add_trace(go.Bar(
  x = x,
  y = [64, 96, 17, 42, 85, 94, 79, 27, 96, 58, 76, 52, 73, 11],
  base = list(map(lambda val: val * -1, [64, 96, 17, 42, 85, 94, 79, 27, 96, 58, 76, 52, 73, 11])),
  text = list(map(lambda val: 'Expense:'+str(val), [64, 96, 17, 42, 85, 94, 79, 27, 96, 58, 76, 52, 73, 11])),
  marker_color='crimson',
  name='expenses'
))

fig.add_trace(go.Bar(
  x = x,
  y = [42, 33, 63, 42, 17, 98, 86, 24, 75, 20, 37, 40, 91, 92], 
  text = list(map(lambda val: 'Revenue:'+str(val), [42, 33, 63, 42, 17, 98, 86, 24, 75, 20, 37, 40, 91, 92])),
  base=0,
  marker_color='lightslategrey',
  name='revenue'
))

fig.update_layout(
    barmode='stack',
    xaxis={'categoryorder':'array', 'categoryarray':x})

fig.show()

Facing the same issue. Has anyone else encountered this?