You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use the `offsetgroup` property with `barmode="stacked"` or `barmode="relative"` to create grouped stacked bar charts. Bars that have the same `offsetgroup` will share the same position on the axis. Bars with no `offsetgroup` set will also share the same position on the axis. In the following example, for each quarter, the value for cities that belong to the same `offsetgroup` are stacked together.
331
+
332
+
```python
333
+
import plotly.graph_objects as go
334
+
335
+
data = [
336
+
go.Bar(
337
+
x=['Q1', 'Q2', 'Q3', 'Q4'],
338
+
y=[150, 200, 250, 300],
339
+
name='New York',
340
+
offsetgroup="USA"
341
+
),
342
+
go.Bar(
343
+
x=['Q1', 'Q2', 'Q3', 'Q4'],
344
+
y=[180, 220, 270, 320],
345
+
name='Boston',
346
+
offsetgroup="USA"
347
+
),
348
+
go.Bar(
349
+
x=['Q1', 'Q2', 'Q3', 'Q4'],
350
+
y=[130, 170, 210, 260],
351
+
name='Montreal',
352
+
offsetgroup="Canada"
353
+
),
354
+
go.Bar(
355
+
x=['Q1', 'Q2', 'Q3', 'Q4'],
356
+
y=[160, 210, 260, 310],
357
+
name='Toronto',
358
+
offsetgroup="Canada"
359
+
)
360
+
]
361
+
362
+
layout = go.Layout(
363
+
title={
364
+
'text': 'Quarterly Sales by City, Grouped by Country'
365
+
},
366
+
xaxis={
367
+
'title': {
368
+
'text': 'Quarter'
369
+
}
370
+
},
371
+
yaxis={
372
+
'title': {
373
+
'text': 'Sales'
374
+
}
375
+
},
376
+
barmode='stack'
377
+
)
378
+
379
+
fig = go.Figure(data=data, layout=layout)
380
+
381
+
fig.show()
382
+
```
383
+
307
384
### Stacked Bar Chart From Aggregating a DataFrame
308
385
309
386
Stacked bar charts are a powerful way to present results summarizing categories generated using the Pandas aggregate commands. `pandas.DataFrame.agg` produces a wide data set format incompatible with `px.bar`. Transposing and updating the indexes to achieve `px.bar` compatibility is a somewhat involved option. Here is one straightforward alternative, which presents the aggregated data as a stacked bar using plotly.graph_objects.
@@ -639,25 +716,6 @@ fig.update_layout(
639
716
fig.show()
640
717
```
641
718
642
-
### Bar Chart with Relative Barmode
643
-
644
-
With "relative" barmode, the bars are stacked on top of one another, with negative values
Set `categoryorder` to `"category ascending"` or `"category descending"` for the alphanumerical order of the category names or `"total ascending"` or `"total descending"` for numerical order of values. [categoryorder](https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-categoryorder) for more information. Note that sorting the bars by a particular trace isn't possible right now - it's only possible to sort by the total values. Of course, you can always sort your data _before_ plotting it if you need more customization.
0 commit comments