title: Stacked bar plot in python / plotly (express): grouping / ordering of bars
body: <p>I have data in a dataframe that I want to plot with a stacked bar plot:</p>
  <pre><code>test_df = pd.DataFrame([[1, 5, 1, 'A'], [2, 10, 1, 'B'], [3, 3, 1, 'A']], columns = ('ID', 'Value', 'Bucket', 'Type'))
  </code></pre>
  <p>if I do the plot with Plotly Express I get bars stacked on each other and correctly ordered (based on the index):</p>
  <pre><code>fig = px.bar(test_df, x='Bucket', y='Value', barmode='stack')
  </code></pre>
  <p>However, I want to color the data based on Type, hence I go for</p>
  <pre><code>fig = px.bar(test_df, x='Bucket', y='Value', barmode='stack', color='Type')
  </code></pre>
  <p><a href="https://i.stack.imgur.com/B6gPl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B6gPl.png" alt="enter image description here" /></a></p>
  <p>This works, except now the ordering is messed up, because all bars are now grouped by Type. I looked through the docs of Plotly Express and couldn't find a way to specify the ordering of the bars independently. Any tips on how to do this?</p>
  <p>I found this one here, but the scenario is a bit different and the options mentioned there don't seem to help me:
  <a href="https://stackoverflow.com/questions/57981337/how-to-disable-plotly-express-from-grouping-bars-based-on-color">How to disable plotly express from grouping bars based on color?</a></p>
  <p>Edit: This goes into the right direction, but not with using Plotly Express, but rather Plotly graph_objects:</p>
  <pre><code>import plotly.graph_objects as go
  test_df = pd.DataFrame([[1, 5, 1, 'A', 'red'], [2, 10, 1, 'B', 'blue'], [3, 3, 1, 'A', 'red']], columns = ('ID', 'Value', 'Bucket', 'Type', 'Color'))
  fig = go.Figure()
  fig.add_trace(go.Bar(x=test_df[&quot;Bucket&quot;], y=test_df[&quot;Value&quot;], marker_color=test_df[&quot;Color&quot;]))
  </code></pre>
  <p>Output:
  <a href="https://i.imgur.com/m9VFNiY.png" rel="nofollow noreferrer"><img src="https://i.imgur.com/m9VFNiY.png" alt="enter image description here" /></a></p>
  <p>Still, I'd prefer the Express version, because so many things are easier to handle there (Legend, Hover properties etc.).</p>