【问题标题】:Python plotting figure on subplot using plotlyPython使用plotly在子图上绘制图形
【发布时间】:2021-08-11 21:23:16
【问题描述】:

我在下面有这个正在工作,但我想在 col=1 和 row=1 的烛台图表内绘制移动平均线

import plotly.graph_objects as go
from plotly.subplots import make_subplots

        fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.03, row_width=[0.3, 0.2, 0.7])
        fig.add_trace(go.Candlestick(x=df.index,open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'], name='Price'), row=1, col=1) ###line need to change
        fig.add_trace(go.Bar(x=df.index, y=df['Volume'], name='Volume'), row=2, col=1)
        fig.add_trace(go.Scatter(x=df.index, y=df['RSI'], line=dict(color="black"), name='RSI'), row=3, col=1)
        fig.add_hrect(y0=0, y1=30, fillcolor="red", opacity=0.25, line_width=0, row=3, col=1)
        fig.add_hrect(y0=70, y1=100, fillcolor="green", opacity=0.25, line_width=0, row=3, col=1)
        fig.update_yaxes(title_text='Price', row=1, col=1)
        fig.update_yaxes(title_text='Volume', row=2, col=1)
        fig.update_yaxes(title_text='RSI', row=3, col=1)

所以我更改了注释 ###line 标记的行需要更改上面的代码,如果单独运行,我验证这些代码可以工作。

    fig = go.Figure(data=[go.Candlestick(x=df.index,
                                     open=df.Open, 
                                     high=df.High,
                                     low=df.Low,
                                     close=df.Close), 
                      go.Scatter(x=df.index, y=df.SMA20, line=dict(color='orange', width=1)),
                      go.Scatter(x=df.index, y=df.SMA50, line=dict(color='green', width=1))])

因此,最终代码如下所示,但不知何故无法正常工作。任何想法?任何线索将不胜感激。谢谢!

import plotly.graph_objects as go
from plotly.subplots import make_subplots

        fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.03, row_width=[0.3, 0.2, 0.7])
        fig.add_trace(go.Figure(data=[go.Candlestick(x=df.index,
                                     open=df.Open, 
                                     high=df.High,
                                     low=df.Low,
                                     close=df.Close), 
                      go.Scatter(x=df.index, y=df.SMA20, line=dict(color='orange', width=1)),
                      go.Scatter(x=df.index, y=df.SMA50, line=dict(color='green', width=1))]), row=1, col=1) ###line need to change
        fig.add_trace(go.Bar(x=df.index, y=df['Volume'], name='Volume'), row=2, col=1)
        fig.add_trace(go.Scatter(x=df.index, y=df['RSI'], line=dict(color="black"), name='RSI'), row=3, col=1)
        fig.add_hrect(y0=0, y1=30, fillcolor="red", opacity=0.25, line_width=0, row=3, col=1)
        fig.add_hrect(y0=70, y1=100, fillcolor="green", opacity=0.25, line_width=0, row=3, col=1)
        fig.update_yaxes(title_text='Price', row=1, col=1)
        fig.update_yaxes(title_text='Volume', row=2, col=1)
        fig.update_yaxes(title_text='RSI', row=3, col=1)

错误是:

ValueError: 
    Invalid element(s) received for the 'data' property of 
        Invalid elements include: [Figure({
    'data': [{'close': array([452.04, 460.04, 459.63, ..., 146.14, 146.09, 145.6 ]),
              'high': array([453.1 , 464.17, 460.  , ..., 147.11, 146.7 , 147.71]),
              'low': array([441.19, 455.71, 452.18, ..., 145.63, 145.52, 145.3 ]),
              'open': array([441.99 , 457.72 , 459.315, ..., 146.35 , 146.2  , 146.44 ]),
              'type': 'candlestick',
              'x': [2020-08-12 00:00:00, 2020-08-13 00:00:00, 2020-08-14 00:00:00,
                    ..., 2021-08-06 00:00:00, 2021-08-09 00:00:00, 2021-08-10
                    00:00:00]},
             {'line': {'color': 'orange', 'width': 1},
              'type': 'scatter',
              'x': [2020-08-12 00:00:00, 2020-08-13 00:00:00, 2020-08-14 00:00:00,
                    ..., 2021-08-06 00:00:00, 2021-08-09 00:00:00, 2021-08-10
                    00:00:00],
              'y': array([409.057 , 412.7545, 416.4705, ..., 146.4395, 146.519 , 146.517 ])},
             {'line': {'color': 'green', 'width': 1},
              'type': 'scatter',
              'x': [2020-08-12 00:00:00, 2020-08-13 00:00:00, 2020-08-14 00:00:00,
                    ..., 2021-08-06 00:00:00, 2021-08-09 00:00:00, 2021-08-10
                    00:00:00],
              'y': array([378.5454, 381.2438, 383.99  , ..., 137.7988, 138.215 , 138.6348])}],
    'layout': {'template': '...'}
})]

    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: ['bar', 'barpolar', 'box', 'candlestick',
                     'carpet', 'choropleth', 'choroplethmapbox',
                     'cone', 'contour', 'contourcarpet',
                     'densitymapbox', 'funnel', 'funnelarea',
                     'heatmap', 'heatmapgl', 'histogram',
                     'histogram2d', 'histogram2dcontour', 'icicle',
                     '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, ...}])

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    在这种情况下,将移动平均线添加为独立图是没有问题的; RSI 指标需要一个新库,因此我们已将其替换为收盘价,因此请更正该项目。该图没有足够的垂直尺寸,因此我们添加了图的高度。

    import plotly.graph_objects as go
    from plotly.subplots import make_subplots
    
    fig = make_subplots(rows=3, cols=1,
                        shared_xaxes=True,
                        vertical_spacing=0.03,
                        row_width=[0.3, 0.2, 0.7]
                       )
    
    fig.add_trace(go.Candlestick(x=df.index,
                                 open=df.Open, 
                                 high=df.High,
                                 low=df.Low,
                                 close=df.Close),row=1, col=1)
    fig.add_trace(go.Scatter(x=df.index, 
                             y=df.SMA20, 
                             line=dict(color='orange', width=1),
                             name='SMA20'
                            ), row=1, col=1)
                  
    fig.add_trace(go.Scatter(x=df.index, 
                             y=df.SMA50, 
                             line=dict(color='green', width=1),
                             name='SMA50'
                            ), row=1, col=1)
    
    fig.add_trace(go.Bar(x=df.index, 
                         y=df['Volume'],
                         name='Volume'), row=2, col=1)
    
    fig.add_trace(go.Scatter(x=df.index, 
                             y=df['Close'],
                             line=dict(color="black"),
                             name='Close'), row=3, col=1)
    
    fig.add_hrect(y0=0, y1=30, 
                  fillcolor="red",
                  opacity=0.25,
                  line_width=0, row=3, col=1)
    
    fig.add_hrect(y0=70, y1=100,
                  fillcolor="green", 
                  opacity=0.25,
                  line_width=0, row=3, col=1)
    
    fig.update_yaxes(title_text='Price', row=1, col=1)
    fig.update_yaxes(title_text='Volume', row=2, col=1)
    fig.update_yaxes(title_text='RSI', row=3, col=1)
    
    fig.update_layout(height=800)
    
    fig.show()
    

    【讨论】:

    • 这是一个很好的利用 plotly 特性的倡议。如果此答案对您有帮助,请勾选复选标记接受答案。
    • 嗨@r-beginners,非常感谢您的帮助......感谢它
    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 2021-11-17
    • 1970-01-01
    • 2020-08-27
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    相关资源
    最近更新 更多