首页 > 解决方案 > `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

标签: pythonpython-3.xplotgraphplotly

解决方案


错误消息对我来说似乎是正确的:您现在在第一行中有一个 2 列图,但是您No subplot specified at grid position (1, 2)正在尝试向row=1, col=2...添加跟踪row=2, col=2原始规范中的行。


推荐阅读