Search code examples
pythonpython-3.xplotlyplotly-python

Scroll between multiple subplots


i have a question regarding subplots. i’m creating plots using this command:

 figure = make_subplots(
        rows=4,
        cols=1,
        shared_xaxes=True,
        subplot_titles=("title1", "title2", "title3", "title4"),
        vertical_spacing=0.1,
        column_titles=[
            f'title'
        ]
    )

# then creating multiple candlestick graphs like this for each row:
figure.add_trace(
    go.Candlestick(
        x=inner_data.index,
        open=inner_data['open'],
        high=inner_data['high'],
        low=inner_data['low'],
        close=inner_data['close'],
        name=f"{ticker} - INNER GRAPH"
    ),
    row=row,
    col=1
)



figure.show()

but because i have too many rows, the plots are shrinking a lot, and i cannot see anything. is there a way to keep the size big, and create a scroll option in the page that opens up? i have not found it anywhere in the documentation…

thanks in advance, Yaniv


Solution

  • A candlestick graph has been created using four stock prices. The display area can be changed by setting the graph height. The unit is pixels. Also, the range slider is not shown as an element that makes the graph area narrower. If necessary, change it to True. Since column titles and sub-titles are covered, the column titles are not set, but are changed to the overall title.

    import yfinance as yf
    tickers = "AAPL TSLA GOOG NFLX"
    df = yf.download(tickers, start="2021-07-01", end="2022-07-01", group_by='ticker')
    df = df.stack(0).reset_index().rename(columns={'level_1':'Ticker'})
    
    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    
    figure = make_subplots(
            rows=4,
            cols=1,
            shared_xaxes=True,
            subplot_titles=tickers.split(' '),
            vertical_spacing=0.1,
            # column_titles=[
            #     f'title'
            # ]
        )
    
    for row,ticker in enumerate(tickers.split(' ')):
        inner_data = df[df['Ticker'] == ticker]
        figure.add_trace(
            go.Candlestick(
                x=inner_data.index,
                open=inner_data['Open'],
                high=inner_data['High'],
                low=inner_data['Low'],
                close=inner_data['Close'],
                name=f"{ticker} - INNER GRAPH"
            ), row=row+1, col=1
        )
        
    figure.update_layout(title='title',
        autosize=True,
        height=800,
    )
    figure.update_xaxes(rangeslider=dict(visible=False))
    figure.show()
    

    enter image description here