Search code examples
pythonplotlysubplot

How to set properties on a row/column in a grid of plotly plots?


Suppose I'm plotting 2 charts on each row, 10 rows, using plotly:

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

N=10
fig = make_subplots(rows=N, cols=2)

fig.add_trace(
    go.Scatter(x=x, y=y),
    row=1, col=1
)

fig.add_trace(
    go.Candlestick(
        x=df_kline.index,
        open=df_kline['O'],
        high=df_kline['H'],
        low=df_kline['L'],
        close=df_kline['C']
    ),
    row=1, col=2
)

:
fig.show()

How can I set a yaxis_title for each row?

How can I set the y-axis range to be [1,10] for the entire first column, and only show the ticklabels at the bottom of the plot?

I hope this qualifies as a single question rather than two, as it's dealing with group-by-row / group-by-col.


FOOTNOTE:

Following from the comments in the accepted answer, one can set settings on multiple subplots thus:

subplot_settings = {
    'rangeslider_visible': True,
    'rangeslider_thickness': 0.05
}
kwargs = {
    f'xaxis{k}' : subplot_settings
        for k in range(2, 2*N, 2)
}
fig.update_layout(**kwargs)

(Untested)


Solution

  • Since no data was presented, I responded to the challenge with four subplots using a certain stock price; the title and range of the y-axis for each row in the first one can be set in the y-axis settings. Also, in the settings section of the subplot, if you set the shared axis to x-axis, only the bottom x-axis will be available.

    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    import numpy as np
    import pandas as pd
    
    x = np.linspace(0,1, 100)
    y = np.cumsum(x)
    
    import yfinance as yf
    df_kline = yf.download("AAPL", start="2021-01-01", end="2021-03-01")
    df_kline.rename(columns={'Open':'O','High':'H','Low':'L','Close':'C'}, inplace=True)
    
    N=2
    fig = make_subplots(rows=N, cols=2, 
                        shared_xaxes=True,    )# vertical_spacing=0.1
    
    fig.add_trace(
        go.Scatter(x=x, y=y),
        row=1, col=1
    )
    
    fig.add_trace(
        go.Candlestick(
            x=df_kline.index,
            open=df_kline['O'],
            high=df_kline['H'],
            low=df_kline['L'],
            close=df_kline['C'],
    
        ),
        row=1, col=2,
    )
    
    fig.add_trace(
        go.Scatter(x=x, y=y),
        row=2, col=1
    )
    
    fig.add_trace(
        go.Candlestick(
            x=df_kline.index,
            open=df_kline['O'],
            high=df_kline['H'],
            low=df_kline['L'],
            close=df_kline['C'],
        ),
        row=2, col=2
    )
    
    fig.update_layout(autosize=False, height=600, width=1000, showlegend=False)
    
    # rangeslider visible false
    fig.update_layout(title='Custome subplots',
                      xaxis2=dict(rangeslider=dict(visible=False)),
                      xaxis4=dict(rangeslider=dict(visible=False)))
    # yxais customize
    fig.update_layout(yaxis1=dict(range=[0,10], title='test'),
                     yaxis3=dict(range=[0,10], title='test2'))
    fig.show()
    

    enter image description here