Pandas - 用分组图表绘制子图
Pandas - Plotly subplots with grouped charts
我有以下两个相似的数据框,其中包含两个不同团队的数据
dataFrameA:
name bat_pos_1 ing_1_runs bat_pos_2 ing_2_runs
jack 2 10 2 20
john 1 20 1 5
dataFrameB:
name bat_pos_1 ing_1_runs bat_pos_2 ing_2_runs
jill 5 20 3 30
nancy 4 5 2 7
我正在使用以下代码来呈现一个多组分组的条形图
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)
我不知道下一步该怎么做。有什么想法吗?
我觉得docs说的很清楚了。定义无花果后,您需要添加每条轨迹并在行中指定,您希望它显示的子图方案的列。
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()
我有以下两个相似的数据框,其中包含两个不同团队的数据
dataFrameA:
name bat_pos_1 ing_1_runs bat_pos_2 ing_2_runs
jack 2 10 2 20
john 1 20 1 5
dataFrameB:
name bat_pos_1 ing_1_runs bat_pos_2 ing_2_runs
jill 5 20 3 30
nancy 4 5 2 7
我正在使用以下代码来呈现一个多组分组的条形图
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)
我不知道下一步该怎么做。有什么想法吗?
我觉得docs说的很清楚了。定义无花果后,您需要添加每条轨迹并在行中指定,您希望它显示的子图方案的列。
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()