question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stacked grouped bars

See original GitHub issue

We need to permit mixed stacked and grouped bars.

We will implement this with a new bar.stackgroup attribute as spec’ed here: https://github.com/plotly/plotly.js/issues/3402

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
RenaudLNcommented, Feb 8, 2022

@araichev @Alexander-Serov Here is a minimum reproducible example with Python. Note that the offsets are suited to this monthly data and will have to be adjusted with other inputs.

import numpy as np
import pandas as pd
import plotly.graph_objects as go


# Create dummy data indexed by month and with multi-columns [product, revenue]
index = pd.date_range("2020", "2021", freq="MS", closed="left")
df = pd.concat(
    [
        pd.DataFrame(
            np.random.rand(12, 3) * 1.25 + 0.25,
            index=index,
            columns=["Revenue1", "Revenue2", "Revenue3"]
        ),
        pd.DataFrame(
            np.random.rand(12, 3) + 0.5,
            index=index,
            columns=["Revenue1", "Revenue2", "Revenue3"]
        ),
    ],
    axis=1,
    keys=["Product1", "Product2"]
)

# Create a figure with the right layout
fig = go.Figure(
    layout=go.Layout(
        height=600,
        width=1000,
        barmode="relative",
        yaxis_showticklabels=False,
        yaxis_showgrid=False,
        yaxis_range=[0, df.groupby(axis=1, level=0).sum().max().max() * 1.5],
       # Secondary y-axis overlayed on the primary one and not visible
        yaxis2=go.layout.YAxis(
            visible=False,
            matches="y",
            overlaying="y",
            anchor="x",
        ),
        font=dict(size=24),
        legend_x=0,
        legend_y=1,
        legend_orientation="h",
        hovermode="x",
        margin=dict(b=0,t=10,l=0,r=10)
    )
)

# Define some colors for the product, revenue pairs
colors = {
    "Product1": {
        "Revenue1": "#F28F1D",
        "Revenue2": "#F6C619",
        "Revenue3": "#FADD75",
    },
    "Product2": {
        "Revenue1": "#2B6045",
        "Revenue2": "#5EB88A",
        "Revenue3": "#9ED4B9",
    }
}

# Add the traces
for i, t in enumerate(colors):
    for j, col in enumerate(df[t].columns):
        if (df[t][col] == 0).all():
            continue
        fig.add_bar(
            x=df.index,
            y=df[t][col],
            # Set the right yaxis depending on the selected product (from enumerate)
            yaxis=f"y{i + 1}",
            # Offset the bar trace, offset needs to match the width
            # The values here are in milliseconds, 1billion ms is ~1/3 month
            offsetgroup=str(i),
            offset=(i - 1) * 1000000000,
            width=1000000000,
            legendgroup=t,
            legendgrouptitle_text=t,
            name=col,
            marker_color=colors[t][col],
            marker_line=dict(width=2, color="#333"),
            hovertemplate="%{y}<extra></extra>"
        )

fig.show()

image

2reactions
araichevcommented, Oct 18, 2021

@RenaudLN or anyone else, can you show us code to produce the figure above or similar?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Grouped and Stacked barplot - The R Graph Gallery
Grouped and Stacked barplot display a numeric value for several entities, organised in groups and subgroups. It is probably better to have a...
Read more >
Grouped and Stacked Bar Chart - Login | Domo
A grouped and stacked bar chart requires three data columns or rows from your DataSet—one for categories, one for the series in each...
Read more >
Draw Stacked Bars within Grouped Barplot (R Example)
This example demonstrates how to create a grouped barplot with stacked bars in R. In the next step, we can use the ggplot,...
Read more >
Stacked-to-Grouped Bars / D3 - Observable
Stacked -to-Grouped Bars ; update = undefined ; yMax = 9.334798505310328 ; y1Max = 12.370355670049321 ; x = ƒ ; y = ƒ ......
Read more >
Create a Clustered AND Stacked column chart in Excel (easy)
The clustered column chart is one of the most commonly used chart types in Excel. · In this chart, the column bars related...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found