【问题标题】:Making Subplots in plotly在 plotly 中制作子图
【发布时间】:2022-01-15 19:44:30
【问题描述】:

任何人都有解决方案如何使直方图成为 ohlc 图的子图,具有相同的 y 轴,但不同的 x 轴?非常感谢。

volume_profile = px.histogram(df, x='Volume', y='Close', nbins=25, orientation='h')
    
ohlc = go.Figure(data=go.Ohlc(x=df['Time'],
                              open=df['Open'],
                              high=df['High'],
                              low=df['Low'],
                              close=df['Close']))
                           
volume_profile.show()
ohlc.show()

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    要创建子图,请使用专用设置。请参阅details。子图通常在 graph_objects 中启用,因此我们将其更改为 go。另外,由于是直方图,所以设置为关闭。

    import plotly.graph_objects as go
    import plotly.express as px
    from plotly.subplots import make_subplots
    import yfinance as yf
    
    df = yf.download("AAPL", start="2021-01-01", end="2021-12-01")
    
    fig = make_subplots(rows=1, cols=2, specs=[[{'type':'histogram'}, {'type':'ohlc'}]])
    
    fig.add_trace(
        go.Histogram(             
                  y=df['Close'],
                  #y=df[['Close']].values,
                  #nbins=25,
                  #orientation='h'
                  ),
               row=1, col=1
    )
    
    fig.add_trace(
          go.Ohlc(x=df.index,
          open=df['Open'],
          high=df['High'],
          low=df['Low'],
          close=df['Close']), row=1, col=2
    )
    
    fig.update_layout(
      height=600,
      width=800,
      title_text="Side By Side Subplots",
      xaxis2=dict(rangeslider=dict(visible=False))
       )
    
    fig.show()
    

    【讨论】:

    • 如果我的回答对你有帮助,请考虑接受它作为正确答案并给它1+,谢谢!
    猜你喜欢
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 2018-09-11
    相关资源
    最近更新 更多