python - `plotly.subplots.make_subplots`中`specs`的意外行为
问题描述
在相关plotly
教程页面中,我们有以下示例:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(
rows=2, cols=2,
specs=[[{}, {}],
[{"colspan": 2}, None]],
subplot_titles=("First Subplot","Second Subplot", "Third Subplot"))
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2]),
row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2]),
row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, 2]),
row=2, col=1)
fig.update_layout(showlegend=False, title_text="Specs with Subplot Title")
fig.show()
生成以下图表:
我试图放置Third Subplot
在第一行和First Subplot
第二Second Subplot
行。我正在使用以下代码(与前面的代码相同,仅specs
适当更改了选项):
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(
rows=2, cols=2,
specs=[[{"colspan": 2}, None],
[{}, {}]],
subplot_titles=("First Subplot","Second Subplot", "Third Subplot"))
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2]),
row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2]),
row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, 2]),
row=2, col=1)
fig.update_layout(showlegend=False, title_text="Specs with Subplot Title")
fig.show()
我收到以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-116-cab6bb97a5c7> in <module>
11
12 fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2]),
---> 13 row=1, col=2)
14 fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, 2]),
15 row=2, col=1)
~/path/lib/python3.6/site-packages/plotly/basedatatypes.py in add_trace(self, trace, row, col, secondary_y)
1613 rows=[row] if row is not None else None,
1614 cols=[col] if col is not None else None,
-> 1615 secondary_ys=[secondary_y] if secondary_y is not None else None,
1616 )
1617
~/path/lib/python3.6/site-packages/plotly/basedatatypes.py in add_traces(self, data, rows, cols, secondary_ys)
1711 if rows is not None:
1712 for trace, row, col, secondary_y in zip(data, rows, cols, secondary_ys):
-> 1713 self._set_trace_grid_position(trace, row, col, secondary_y)
1714
1715 # Make deep copy of trace data (Optimize later if needed)
~/path/lib/python3.6/site-packages/plotly/basedatatypes.py in _set_trace_grid_position(self, trace, row, col, secondary_y)
1792 grid_ref = self._validate_get_grid_ref()
1793 return _set_trace_grid_reference(
-> 1794 trace, self.layout, grid_ref, row, col, secondary_y
1795 )
1796
~/path/lib/python3.6/site-packages/plotly/subplots.py in _set_trace_grid_reference(trace, layout, grid_ref, row, col, secondary_y)
1317 """
1318 No subplot specified at grid position ({row}, {col})""".format(
-> 1319 row=row, col=col
1320 )
1321 )
ValueError:
No subplot specified at grid position (1, 2)
我尝试了不同的选择specs
并且他们已经工作了,但是这个我无法工作。我是否以错误的方式提供论点,或者这可能是一个已知问题plotly
?
解决方案
错误消息对我来说似乎是正确的:您现在在第一行中有一个 2 列图,但是您No subplot specified at grid position (1, 2)
正在尝试向row=1, col=2
...添加跟踪row=2, col=2
原始规范中的行。
推荐阅读
- amazon-web-services - 使用 aws cli 的 Jenkins Groovy 脚本和 dynamodb 查询不起作用
- python - AttributeError: 'tuple' 对象没有属性 'lower' sentiword
- reactjs - 未捕获的错误:反应中的重新渲染过多
- sql - sql中最大值的计数
- haskell - 如何在 Haskell 中重构这些 IO 操作?
- parsing - 用于解析不是关键字的标识符名称的 PEG 语法
- css - 使用 PrimeNG 将对话框弹出窗口定位为侧边栏
- flutter - 获取另一个小部件中变量的值
- environment-variables - 如何为某个命令设置环境变量?
- ios - 在 xCode 中禁用链接预览