How to plot a grouped stacked bar chart in plotly

Moritz Körber
1 min readAug 22, 2020

--

plotly makes it easy to create an interactive stacked or grouped bar chart in Python by assigning the desired type to the layout attribute barmode. Unfortunately, barmode only takes either stack or group but not both as an argument. It seems like they are currently working on this option, but there is a workaround for now: subcategory axes!

Let’s say you compare two design layouts of your online store — modern and classic — in a small A/B test. You collect data for three consecutive weeks and count the number of conversions and exits per layout and week. Let’s convert this information into a data frame:

To plot this, we need a categorical x-axis that shows the week, stacked bars to show the number of conversions and exits, and those bars grouped into modern and classic. Since barmode cannot be stack and group at the same time, the grouping must be done by sub-categories. Simply pass a list in the form of [category, sub-category] to x, leave barmode at "stack" and add a trace per response category (here: conversion and exit).

And here it is:

--

--

Moritz Körber

Data science engineering stats psychology sponge working @ Zeiss Digital Partners / “I write to discover what I know.” ― Flannery O’Connor