为了账号安全,请及时绑定邮箱和手机立即绑定

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条经验 获得超5个赞

问题是您如何添加第一条轨迹。您正在使用:


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 关注
  • 262 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号