亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PlotLy:無法在子圖上繪制燭臺圖

PlotLy:無法在子圖上繪制燭臺圖

慕斯709654 2022-11-18 20:41:27
我正在嘗試繪制一個 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/
查看完整描述

1 回答

?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

問題是您如何添加第一條軌跡。您正在使用:


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


但它應該是:


fig.add_trace(go.Candlestick(x = df.index,...


import numpy as np

import pandas as pd

import plotly.graph_objects as go

from plotly.subplots import make_subplots


N=14

df = 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 fig


chart_strategy(df)


查看完整回答
反對 回復 2022-11-18
  • 1 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號