ValueError: The data property of a figure may only be assigned a list or tuple that contains a .....

ValueError: The data property of a figure may only be assigned a list or tuple that contains a permutation of a subset of itself.

问题:

import plotly as py  
# 导入plotly库并命名为py
import plotly.graph_objs as go
import pandas as pd

fig = go.Figure()

# -------------pre def
pyplt = py.offline.plot

df = pd.read_csv(r'dat/appl.csv', index_col=['date'], parse_dates=['date'])

trace = go.Ohlc(
    x=df.index,
    open=df.open,
    high=df.high,
    low=df.low,
    close=df.close,
)

fig['data'] = [trace]
# fig.add_trace(trace)

fig['layout'].update(
    xaxis=go.XAxis(
        autorange=True,
        mirror='all',
        gridcolor='rgb(180, 180, 180)',
        showline=True,  # 画出 X 轴那条线
        showgrid=True,
        tickangle=-60,
        categoryorder="category ascending",
        type='category'),
    yaxis=go.YAxis(
        autorange=True,
        gridcolor='rgb(180, 180, 180)',
    ),
)

pyplt(fig, filename=r'tmp/ohlc_filter_time.html')

 解决:

# fig.add_trace(trace)

import plotly as py  
# 导入plotly库并命名为py
import plotly.graph_objs as go
import pandas as pd

fig = go.Figure()

# -------------pre def
pyplt = py.offline.plot

df = pd.read_csv(r'dat/appl.csv', index_col=['date'], parse_dates=['date'])

trace = go.Ohlc(
    x=df.index,
    open=df.open,
    high=df.high,
    low=df.low,
    close=df.close,
)

# fig['data'] = [trace]
fig.add_trace(trace)

fig['layout'].update(
    xaxis=go.XAxis(
        autorange=True,
        mirror='all',
        gridcolor='rgb(180, 180, 180)',
        showline=True,  # 画出 X 轴那条线
        showgrid=True,
        tickangle=-60,
        categoryorder="category ascending",
        type='category'),
    yaxis=go.YAxis(
        autorange=True,
        gridcolor='rgb(180, 180, 180)',
    ),
)

pyplt(fig, filename=r'tmp/ohlc_filter_time.html')

完整错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-9eb74208098f> in <module>
     19 )
     20 
---> 21 fig['data'] = [trace]
     22 # fig.add_trace(trace)
     23 

D:\anaconda\lib\site-packages\plotly\basedatatypes.py in __setitem__(self, prop, value)
    682 
    683             if prop == "data":
--> 684                 self.data = value
    685             elif prop == "layout":
    686                 self.layout = value

D:\anaconda\lib\site-packages\plotly\basedatatypes.py in __setattr__(self, prop, value)
    719         if prop.startswith("_") or hasattr(self, prop):
    720             # Let known properties and private properties through
--> 721             super(BaseFigure, self).__setattr__(prop, value)
    722         else:
    723             # Raise error on unknown public properties

D:\anaconda\lib\site-packages\plotly\basedatatypes.py in data(self, new_data)
   1003             err_msg = err_header
   1004 
-> 1005             raise ValueError(err_msg)
   1006 
   1007         # ### Check for duplicates in assignment ###

ValueError: The data property of a figure may only be assigned 
a list or tuple that contains a permutation of a subset of itself.

参考:plotly

参考:How to update figure data?

上一篇:10、HTTP请求方法你知道多少?


下一篇:Coresight(一)coresight简介