[英]how to plot different types of charts using plotly and make_subplot in python
[英]how to plot multiple charts using make_subplot in plotly and python
我正在嘗試使用plotly和make_subplot繪制多個圖表,我想在其中繪制使用迭代 for 循環創建的 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
問題在於當我分配行和列並嘗試使用規范參數時,系統崩潰並顯示以下錯誤。
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 個餅圖和 1 個條形圖
樣例數據增加了新的屬性數據,修改了行和列的計算邏輯。 此外,最后一個條形圖現在是單個圖,因為圖的數量與循環數據不匹配。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.