Plotly express bar ignores barmode='group'

Hi,

I am new to plotly. I am trying to do a bar plot with bars grouped side-by side. Using the python and plotly express bar, the argument barmode='group' has no effect and the bar are stacked instead. Here is the code:

fig = px.bar(df_, x='model_name', y='num_items', color='model_cycle', barmode='group')

Output:

For completeness, here is the DataFrame:

dd = {'model_name': {0: 1,
  1: 5,
  2: 5,
  3: 6,
  4: 2,
  5: 2,
  6: 2,
  7: 2,
  8: 4,
  9: 4,
  10: 0,
  11: 3,
  12: 3,
  13: 3},
 'model_cycle': {0: 1,
  1: 1,
  2: 2,
  3: 1,
  4: 1,
  5: 2,
  6: 3,
  7: 4,
  8: 1,
  9: 2,
  10: 1,
  11: 1,
  12: 2,
  13: 3},
 'num_items': {0: 2804,
  1: 1966,
  2: 5835,
  3: 1034,
  4: 900,
  5: 7,
  6: 1973,
  7: 2108,
  8: 416,
  9: 1137,
  10: 285,
  11: 1808,
  12: 4817,
  13: 2835}}
df_ = pd.DataFrame.from_dict(dd)

Hi,
px is interpreting the model_cycle column as continuous values (also see the colorbar) and, in this case, falls back to stacked bar chart. You can avoid this problem by having str values for the color, with

df_['model_cycle'] = df_['model_cycle'].astype(str)

(or create a new column if you donโ€™t want to overwrite it). However the current behaviour is misleading, a warning should be issued in this case.

Hi Emmanuelle,

thanks for the quick answer. You fixed both my problems in one shot :wink: (the continuous colorbar looked weird, but I assumed that was the default in plotly, so I didnโ€™t asked).

Let me know if I need to open and issue regarding the interpretation of an integer dtype as float.

While I am at it, is there a way to change the legend to have the variable name (model_cycle) as title and only the values (1, 2, 3, 4) as legend items? This would match seabornโ€™s behavior and is less verbose.

Thanks!

Legend titles are not possible yes but weโ€™re working on it. Stay tuned, it should be included in one of next few releases!

Perfect, thanks for the info, I will monitor the release notes closely then.