使用plotly在子图中堆叠条形

问题描述 投票:0回答:1

我有下面的代码,但我现在尝试将两个子图采用 2 行、1 列格式(因此一个子图位于另一个子图下方)。不幸的是,当我尝试这样做时,我最终得到的条形图不再堆叠。有没有办法用情节来做到这一点?

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()

这是我的尝试 - 正如你所看到的,酒吧最终不再堆叠起来:

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()
python plotly
1个回答
0
投票

出于某种原因,在第二个代码中,您删除了

barmode='stack'
,这就是使列堆叠的原因

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()

© www.soinside.com 2019 - 2024. All rights reserved.