Pandas - 使用分组图表绘制子图

尼尔

我有以下两个类似的数据框,其中包含两个不同团队的数据

数据帧A:

name         bat_pos_1 ing_1_runs bat_pos_2 ing_2_runs
jack           2           10            2         20
john           1           20            1         5

数据帧B:

name         bat_pos_1 ing_1_runs bat_pos_2 ing_2_runs
jill           5           20            3         30
nancy          4            5            2         7

我正在使用以下代码来呈现一个 poltly 分组条形图

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

figTeamARuns = go.Figure(data=[
    go.Bar(name='Ist Inning', x=dataFrameA['name'], y=dataFrameA['ing_1_runs']),
    go.Bar(name='2nd Inning', x=dataFrameA['name'], y=dataFrameA['ing_2_runs']),
   ])
figTeamARuns.update_layout(barmode='group')
figTeamARuns.show()

figTeamBRuns = go.Figure(data=[
    go.Bar(name='Ist Inning', x=dataFrameB['name'], y=dataFrameB['ing_1_runs']),
    go.Bar(name='2nd Inning', x=dataFrameB['name'], y=dataFrameB['ing_2_runs']),
   ])
figTeamBRuns.update_layout(barmode='group')
figTeamBRuns.show()

上面的代码呈现两个图表,一个在另一个下方,但我希望相同的图表并排显示。我知道我可以使用 plotly subplots 来完成,但我不知道如何去做。这是我正在尝试使用的代码

figFinalBatting = go.Figure()
figFinalBatting = make_subplots(rows=1, cols=2)

我被困在下一步该做什么上。有任何想法吗?

帕奈

我认为文档在这里很清楚。定义 fig 后,您需要添加每条跟踪并在您希望它出现的子图方案的 row, col 中指定。

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=1, cols=2)
fig.add_trace(
    go.Bar(name='Ist Inning',
           x=dataFrameA['name'],
           y=dataFrameA['ing_1_runs']),
    row=1, col=1)

fig.add_trace(
    go.Bar(name='2nd Inning',
           x=dataFrameA['name'],
           y=dataFrameA['ing_2_runs']),
    row=1, col=1)

fig.add_trace(
    go.Bar(name='Ist Inning',
           x=dataFrameB['name'],
           y=dataFrameB['ing_1_runs']),
    row=1, col=2)

fig.add_trace(
    go.Bar(name='2st Inning',
           x=dataFrameB['name'],
           y=dataFrameB['ing_2_runs']),
    row=1, col=2)

fig.show()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章