ValueError:收到的“数据”属性无效元素

问题描述 投票:0回答:1

我遇到了与密谋有关的问题。我想显示不同的数字,但是,以某种方式,我无法实现我想要的。

我创建了2个数据源:

from plotly.graph_objs.scatter import Line
import plotly.graph_objs as go

trace11 = go.Scatter(
    x = [0, 1, 2],
    y = [0, 0, 0],
    line = Line({'color': 'rgb(0, 0, 128)', 'width': 1})
)

trace12 = go.Scatter(
    x=[0, 1, 2],
    y=[1, 1, 1],
    line = Line({'color': 'rgb(128, 0, 0)', 'width': 1})
)

trace21 = go.Scatter(
    x = [0, 1, 2],
    y = [0.5, 0.5, 0.5],
    line = Line({'color': 'rgb(0, 0, 128)', 'width': 1})
)

trace22 = go.Scatter(
    x=[0, 1, 2],
    y=[1.5, 1.5, 1.5],
    line = Line({'color': 'rgb(128, 0, 0)', 'width': 1})
)

data1 = [trace11, trace12]
data2 = [trace21, trace22]

然后,我创建了一个具有1行和2列的子图,并试图将此数据添加到该子图:

from plotly import tools
fig = tools.make_subplots(rows=1, cols=2)
fig.append_trace(data1, 1, 1)
fig.append_trace(data2, 1, 2)
fig.show()

导致以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-ba20e4900d41> in <module>
      1 from plotly import tools
      2 fig = tools.make_subplots(rows=1, cols=2)
----> 3 fig.append_trace(data1, 1, 1)
      4 fig.append_trace(data2, 1, 2)
      5 fig.show()

~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in append_trace(self, trace, row, col)
   1797         )
   1798 
-> 1799         self.add_trace(trace=trace, row=row, col=col)
   1800 
   1801     def _set_trace_grid_position(self, trace, row, col, secondary_y=False):

~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in add_trace(self, trace, row, col, secondary_y)
   1621             rows=[row] if row is not None else None,
   1622             cols=[col] if col is not None else None,
-> 1623             secondary_ys=[secondary_y] if secondary_y is not None else None,
   1624         )
   1625 

~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in add_traces(self, data, rows, cols, secondary_ys)
   1684 
   1685         # Validate traces
-> 1686         data = self._data_validator.validate_coerce(data)
   1687 
   1688         # Set trace indexes

~\Anaconda3\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v, skip_invalid)
   2667 
   2668             if invalid_els:
-> 2669                 self.raise_invalid_elements(invalid_els)
   2670 
   2671             v = to_scalar_or_list(res)

~\Anaconda3\lib\site-packages\_plotly_utils\basevalidators.py in raise_invalid_elements(self, invalid_els)
    296                     pname=self.parent_name,
    297                     invalid=invalid_els[:10],
--> 298                     valid_clr_desc=self.description(),
    299                 )
    300             )

ValueError: 
    Invalid element(s) received for the 'data' property of 
        Invalid elements include: [[Scatter({
    'line': {'color': 'rgb(0, 0, 128)', 'width': 1}, 'x': [0, 1, 2], 'y': [0, 0, 0]
}), Scatter({
    'line': {'color': 'rgb(128, 0, 0)', 'width': 1}, 'x': [0, 1, 2], 'y': [1, 1, 1]
})]]

    The 'data' property is a tuple of trace instances
    that may be specified as:
      - A list or tuple of trace instances
        (e.g. [Scatter(...), Bar(...)])
      - A single trace instance
        (e.g. Scatter(...), Bar(...), etc.)
      - A list or tuple of dicts of string/value properties where:
        - The 'type' property specifies the trace type
            One of: ['area', 'bar', 'barpolar', 'box',
                     'candlestick', 'carpet', 'choropleth',
                     'choroplethmapbox', 'cone', 'contour',
                     'contourcarpet', 'densitymapbox', 'funnel',
                     'funnelarea', 'heatmap', 'heatmapgl',
                     'histogram', 'histogram2d',
                     'histogram2dcontour', 'image', 'indicator',
                     'isosurface', 'mesh3d', 'ohlc', 'parcats',
                     'parcoords', 'pie', 'pointcloud', 'sankey',
                     'scatter', 'scatter3d', 'scattercarpet',
                     'scattergeo', 'scattergl', 'scattermapbox',
                     'scatterpolar', 'scatterpolargl',
                     'scatterternary', 'splom', 'streamtube',
                     'sunburst', 'surface', 'table', 'treemap',
                     'violin', 'volume', 'waterfall']

        - All remaining properties are passed to the constructor of
          the specified trace type

        (e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])

我很想做错什么了。奇怪的是我的数据看起来正确,因为我可以运行以下代码而不会出现任何问题:

fig = go.Figure(data1)
fig.show()

希望您能帮助我找到解决方案。

谢谢!

python plotly
1个回答
0
投票

我正在遇到类似的问题。我的程序从[]跳转

fig = make_subplots(rows=1, cols=3, shared_yaxes=True,
                        specs=[[{}, {"colspan": 2}, None]])

也为def to_scalar_or_list(v):

您找到问题了吗?

© www.soinside.com 2019 - 2024. All rights reserved.