0

Anyone has solution on how to make make the histogram graph a subplot to the ohlc graph, with same y-axis, but different x-axises? Thanks a lot.

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()

output

1 Answer 1

2

To create a subplot, use the dedicated settings. See this for details. Subplots are usually enabled in graph_objects, so we have changed it to go. Also, since it is a histogram, it is set to close.

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()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

If my answer helped you, please consider accepting it as the correct answer and give it 1+, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.