Search code examples
pythonplotly

Stacked bars in subplots with plotly


I have the code below but I am now trying to have the two subplots in a 2 rows, 1 column format (so one subplot underneath the other). Unfortunately, when I try to do this, I end up have my bar plot with bars not being stacked anymore. Is there a way to to this with plotly?

import plotly.graph_objects as go

# Sample data for the stacked bar plot
categories = ['Category 1', 'Category 2', 'Category 3']
values1 = [10, 20, 15]
values2 = [5, 15, 10]
values3 = [15, 10, 5]

# Sample data for the line plot
x = [1, 2, 3]
y = [30, 40, 35]

# Create a subplot with a stacked bar plot
fig = go.Figure()

fig.add_trace(go.Bar(x=categories, y=values1, name='Value 1'))
fig.add_trace(go.Bar(x=categories, y=values2, name='Value 2'))
fig.add_trace(go.Bar(x=categories, y=values3, name='Value 3'))

# Create a subplot with a line plot
fig.add_trace(go.Scatter(x=x, y=y, mode='lines+markers', name='Line Plot'))

# Set layout for the entire figure
fig.update_layout(
    title='Stacked Bar Plot and Line Plot',
    barmode='stack',  # To create a stacked bar plot
    xaxis=dict(title='Categories'),  # X-axis title
    yaxis=dict(title='Values'),      # Y-axis title
)

# Show the plot
fig.show()

Here is my tentative - as you can see the bars end up not being stacked anymore:

import plotly.subplots as sp
import plotly.graph_objects as go

# Create a 2x1 subplot grid
fig = sp.make_subplots(rows=2, cols=1)

# Data for the stacked bar plot
categories = ['Category A', 'Category B', 'Category C']
values1 = [10, 20, 15]
values2 = [5, 15, 10]

# Create the stacked bar plot
bar_trace1 = go.Bar(x=categories, y=values1, name='Trace 1')
bar_trace2 = go.Bar(x=categories, y=values2, name='Trace 2')
fig.add_trace(bar_trace1, row=1, col=1)
fig.add_trace(bar_trace2, row=1, col=1)

# Data for the line plot
x_values = [1, 2, 3, 4, 5]
y_values = [3, 5, 8, 4, 9]

# Create the line plot
line_trace = go.Scatter(x=x_values, y=y_values, mode='lines', name='Line Plot')
fig.add_trace(line_trace, row=2, col=1)

# Update layout
fig.update_layout(title='Stacked Bar and Line Plot',
                  xaxis_title='X-Axis Label',
                  yaxis_title='Y-Axis Label')

# Show the plot
fig.show()

Solution

  • For some unknown reason in your second code you removed the barmode='stack' which is what actually makes the columns stack.

    Putting this back again in the code solves your question

    import plotly.subplots as sp
    import plotly.graph_objects as go
    
    # Create a 2x1 subplot grid
    fig = sp.make_subplots(rows=2, cols=1)
    
    # Data for the stacked bar plot
    categories = ['Category A', 'Category B', 'Category C']
    values1 = [10, 20, 15]
    values2 = [5, 15, 10]
    
    # Create the stacked bar plot
    bar_trace1 = go.Bar(x=categories, y=values1, name='Trace 1')
    bar_trace2 = go.Bar(x=categories, y=values2, name='Trace 2')
    fig.add_trace(bar_trace1, row=1, col=1)
    fig.add_trace(bar_trace2, row=1, col=1)
    
    # Data for the line plot
    x_values = [1, 2, 3, 4, 5]
    y_values = [3, 5, 8, 4, 9]
    
    # Create the line plot
    line_trace = go.Scatter(x=x_values, y=y_values, mode='lines', name='Line Plot')
    fig.add_trace(line_trace, row=2, col=1)
    
    # Update layout
    fig.update_layout(title='Stacked Bar and Line Plot',
                      barmode='stack',
                      xaxis_title='X-Axis Label',
                      yaxis_title='Y-Axis Label')
    
    # Show the plot
    fig.show()
    

    enter image description here