PlotLy:无法在子图上绘制烛台图

我正在尝试绘制一个 3 子图 PlotLy 图。从上到下:烛台、法线图、法线图。


这是我的代码:


def chart_strategy(df):


    # Initialize figure with subplots

    fig = make_subplots(rows=3, cols=1)


    fig.add_trace(go.Figure( data = [go.Candlestick( x = df.index,

                                                    open = df['Open'],

                                                    close = df['Close'],

                                                    low = df['Low'],

                                                    high = df['High'])]),

                 row = 1, col = 1)



    fig.add_trace(go.Scatter(x = df.index, y = df['System Quality Number']), row = 2, col = 1)


    fig.add_trace(go.Scatter(x = df.index, y = df['Action']), row = 3, col =1)


    return fig


我收到以下错误:


ValueError: 

Invalid element(s) received for the 'data' property of 

    Invalid elements include: [Figure({

'data': [{'close': array([120.88, 120.93, 120.72, ..., 116.23, 115.78, 115.63]),

          'high': array([121.31, 121.2 , 121.11, ..., 116.86, 116.43, 115.73]),

          'low': array([120.8 , 120.79, 120.48, ..., 115.94, 115.68, 115.32]),

          'open': array([121.  , 120.81, 120.92, ..., 116.36, 116.23, 115.71]),

          'type': 'candlestick',

          'x': array([datetime.datetime(2019, 7, 19, 0, 0),

                      datetime.datetime(2019, 7, 22, 0, 0),

                      datetime.datetime(2019, 7, 23, 0, 0), ...,

                      datetime.datetime(2020, 5, 12, 0, 0),

                      datetime.datetime(2020, 5, 13, 0, 0),

                      datetime.datetime(2020, 5, 14, 0, 0)], dtype=object)}],

'layout': {'template': '...'}})]

我尝试只单独绘制烛台图表并且效果很好所以我很漂亮它与行和列部分有关,只是不确定接下来要搜索什么。


我使用以下资源编写此代码: https ://plotly.com/python/table-subplots/ https://plotly.com/python/candlestick-charts/


慕斯709654
浏览 263回答 1
1回答

慕娘9325324

问题是您如何添加第一条轨迹。您正在使用:fig.add_trace(go.Figure( data = [go.Candlestick( x = df.index,...但它应该是:fig.add_trace(go.Candlestick(x = df.index,...import numpy as npimport pandas as pdimport plotly.graph_objects as gofrom plotly.subplots import make_subplotsN=14df = pd.DataFrame({'Open': np.random.randint(1,29,N),                   'Close': np.random.randint(1,29,N),                   'Low': np.random.randint(1,29,N),                   'High': np.random.randint(1,29,N),                   'Action': np.random.choice(['sell', 'buy'],N),                   'System Quality Number': np.random.randint(1,29,N)})def chart_strategy(df):    fig = make_subplots(rows=3, cols=1)    fig.add_trace(go.Candlestick(x = df.index,                                open = df['Open'],                                close = df['Close'],                                low = df['Low'],                                high = df['High']),                 row = 1, col = 1)    fig.add_trace(go.Scatter(x = df.index, y = df['System Quality Number']),                              row = 2, col = 1)    fig.add_trace(go.Scatter(x = df.index, y = df['Action']), row = 3, col =1)    fig.update_xaxes(row=1, col=1, rangeslider_thickness=0.05)    fig.update_layout(width=900, height=900)    return figchart_strategy(df)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python