gpt4 book ai didi

python - 如何在 plotly 和 python 中使用 make_subplot 绘制多个图表

转载 作者:行者123 更新时间:2023-12-01 23:43:12 29 4
gpt4 key购买 nike

我正在尝试使用 plotlyma​​ke_subplot 绘制多个图表,我想绘制 5 个使用迭代创建的饼图环形。和 1 个条形图,其中条形图将采用 colspan = 2

数据框示例:

eventtype_name    date    eventmohafaza_name   number       groups
watch movie 8/14/2020 loc1 25 group1
stay at home 8/13/2020 loc2 20 group2
swimming 8/13/2020 loc2 25 group4
camping 8/13/2020 loc3 20 group3
meeting 8/13/2020 loc4 40 group5
meeting 8/13/2020 loc5 20 group2
watch movie 8/14/2020 loc4 25 group1
stay at home 8/13/2020 loc1 20 group2
swimming 8/13/2020 loc5 25 group4
camping 8/13/2020 loc4 20 group3
meeting 8/13/2020 loc3 30 group5
meeting 8/13/2020 loc5 33 group2

问题是当我分配并尝试使用specs参数时系统崩溃并显示以下错误。

This is the format of your plot grid:
[ (1,1) ] [ (1,2) ] [ (1,3) ]
[ (2,1) ] [ (2,2) ] [ (2,3) ]
[ (3,1) ] [ (3,2) x,y - ]

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
f:\aienv\lib\site-packages\plotly\subplots.py in _set_trace_grid_reference(trace, layout, grid_ref, row, col, secondary_y)
1318 try:
-> 1319 subplot_refs = grid_ref[row - 1][col - 1]
1320 except IndexError:

IndexError: list index out of range

During handling of the above exception, another exception occurred:

Exception Traceback (most recent call last)
<ipython-input-108-831d326e87b8> in <module>
59 rotation=90),
60 row=row,
---> 61 col=col
62 )
63

f:\aienv\lib\site-packages\plotly\basedatatypes.py in add_trace(self, trace, row, col, secondary_y)
1650 rows=[row] if row is not None else None,
1651 cols=[col] if col is not None else None,
-> 1652 secondary_ys=[secondary_y] if secondary_y is not None else None,
1653 )
1654

f:\aienv\lib\site-packages\plotly\basedatatypes.py in add_traces(self, data, rows, cols, secondary_ys)
1764 if rows is not None:
1765 for trace, row, col, secondary_y in zip(data, rows, cols, secondary_ys):
-> 1766 self._set_trace_grid_position(trace, row, col, secondary_y)
1767
1768 # Make deep copy of trace data (Optimize later if needed)

f:\aienv\lib\site-packages\plotly\basedatatypes.py in _set_trace_grid_position(self, trace, row, col, secondary_y)
1847 grid_ref = self._validate_get_grid_ref()
1848 return _set_trace_grid_reference(
-> 1849 trace, self.layout, grid_ref, row, col, secondary_y
1850 )
1851

f:\aienv\lib\site-packages\plotly\subplots.py in _set_trace_grid_reference(trace, layout, grid_ref, row, col, secondary_y)
1320 except IndexError:
1321 raise Exception(
-> 1322 "The (row, col) pair sent is out of "
1323 "range. Use Figure.print_grid to view the "
1324 "subplot grid. "

Exception: The (row, col) pair sent is out of range. Use Figure.print_grid to view the subplot grid.

代码:

import plotly.graph_objs as go
from plotly.subplots import make_subplots
import plotly.express as px
import pandas as pd



df = pd.read_excel(file)
lst = list(df.groupby('eventmohafaza_name'))


rows = 3
cols = 3

subplot_titles = [l[0] for l in lst]


specs=[
[{"type": "pie"},{"type": "pie"},{"type": "pie"}],
[{"type": "pie"},{"type": "pie"},{"type": "pie"}],
[{"type": "pie"},{"type": "bar",'colspan':2},None],
]


fig = make_subplots(
rows=rows,
cols=cols,
subplot_titles=subplot_titles,
specs=specs,

print_grid=True
)

for i, l in enumerate(lst):
row = i // cols + 1
col = i % (rows + 1) + 1
d = l[1]

fig.add_trace(go.Pie(labels=d["eventtype_name"],
values = d["number"],
hovertemplate = "%{label}: <br>Value: %{value} ",
showlegend=True,
textposition='inside',
rotation=90),
row=row,
col=col
)

group_event_groups = df.groupby([df['eventmohafaza_name'],df['eventtype_name']]).size().unstack()
for x in group_event_groups.columns:
fig.add_trace(go.Bar(name=str(x), x=group_event_groups.index, y=group_event_groups[x],showlegend=False), row=3, col=2)

fig.update_layout(height = 1000,
width = 900,
autosize = False,
title="Population by Continent",
title_x=0.5)
fig.show()

预期结果是5张饼图和一张条形图

最佳答案

示例数据新增属性数据,修改rows和cols的计算逻辑。此外,最后一个条形图现在是单个图,因为图的数量与循环数据不匹配。

lst = list(df.groupby('eventmohafaza_name'))

rows = 3
cols = 3

subplot_titles = [l[0] for l in lst]

specs=[[{"type": "pie"},{"type": "pie"},{"type": "pie"}],
[{"type": "pie"},{"type": "pie"},{"type": "pie"}],
[{"type": "pie"},{"type": "bar",'colspan':2},None]]

fig = make_subplots(
rows=rows,
cols=cols,
subplot_titles=subplot_titles,
specs=specs,
print_grid=True
)

for i, l in enumerate(lst):
row = i // cols + 1
col = (i % rows) + 1
d = l[1]
fig.add_trace(go.Pie(labels=d["eventtype_name"],
values = d["number"],
hovertemplate = "%{label}: <br>Value: %{value} ",
showlegend=True,
textposition='inside',
rotation=90),
row=row,
col=col
)

group_event_groups = df.groupby([df['eventmohafaza_name'],df['eventtype_name']]).size().unstack()
# for x in group_event_groups.columns:
fig.add_trace(go.Bar(name='camping', x=group_event_groups.index, y=group_event_groups.columns, showlegend=False), row=3, col=2)

fig.update_layout(height = 1000,
width = 900,
autosize = False,
title="Population by Continent",
title_x=0.5)
fig.show()

enter image description here

关于python - 如何在 plotly 和 python 中使用 make_subplot 绘制多个图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64710320/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com