Search code examples
pythonplotlysubplotcandlestick-chart

Plotly: How to plot candlestick charts on a subplot?


I am trying to plot a subplot which contains 14 candlestick charts of cryptocurrency data. ( https://www.kaggle.com/c/g-research-crypto-forecasting)

However, it seems that it can't display the figure properly.

Here is my code:

from plotly import subplots
import plotly.graph_objects as go

fig = subplots.make_subplots(rows=7,cols=2)

for ix,coin_name in enumerate(asset_details["Asset_Name"]):
   coin_df = crypto_df[crypto_df["Asset_ID"]==asset_names_dict[coin_name]].set_index("timestamp")
   coin_df_mini = coin_df.iloc[-100:]
   column = lambda ix: 1 if ix % 2 == 0 else 2
   candlestick = go.Candlestick(x=coin_df_mini.index, open=coin_df_mini['Open'], high=coin_df_mini['High'], low=coin_df_mini['Low'], close=coin_df_mini['Close'])
   fig = fig.add_trace(candlestick, row=((ix//2) + 1), col=column(ix))
   fig.update_layout(xaxis_rangeslider_visible=False)

fig.update_layout(title_text="Candlestick Charts", height=2800)
fig.show()

And here is the problem:

rangeslider_problem

No matter I plot the figure with or without the rangeslider, it's always out of shape.


Solution

  • You need to hide the slider on the x-axis unit created in the subplot. My answer was to do all the subplots manually. I don't have time to deal with this right now, but there is also a way to update the output content in a loop process.

    fig.update_layout(xaxis1=dict(rangeslider=dict(visible=False)),
                      xaxis2=dict(rangeslider=dict(visible=False)),
                      xaxis3=dict(rangeslider=dict(visible=False)),
                      xaxis4=dict(rangeslider=dict(visible=False)),
                      xaxis5=dict(rangeslider=dict(visible=False)),
                      xaxis6=dict(rangeslider=dict(visible=False)),
                      xaxis7=dict(rangeslider=dict(visible=False)),
                      xaxis8=dict(rangeslider=dict(visible=False)),
                      xaxis9=dict(rangeslider=dict(visible=False)),
                      xaxis10=dict(rangeslider=dict(visible=False)),
                      xaxis11=dict(rangeslider=dict(visible=False)),
                      xaxis12=dict(rangeslider=dict(visible=False)),
                      xaxis13=dict(rangeslider=dict(visible=False)),
                      xaxis14=dict(rangeslider=dict(visible=False)),
                     )
    

    enter image description here