How to subplot heatmap with side bar in right position?

I want a simple way to plot multiple figures onto subplots.
Below is my code , but there is a problem that right colorbar on each figure can not put in right position, they are overlaped. You know each heatmap contain different values .
Create each trace manually, and set to subplot is very time consuming , that’s why I want to reuse the figure , directly draw on subplots. So what should I do to the corlorbar ?

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


cols = ['total_trades', 'max_dd', 'win_rate', 'total_return', 'sharpe_ratio', 'calmar_ratio', ]

cn=3
rn=int(np.ceil(len(cols)/cn))

pad_titles = cols +  (cn*rn - len(cols)) * ['']

fig = make_subplots(rows=rn, cols=cn, subplot_titles=pad_titles)

for i, c in enumerate(cols):
    f = s[c].vbt.heatmap()
    # print(i//cn+1,i%cn+1)
    for trace in f.data:
        fig.add_trace(trace, row=i//cn+1, col=i%cn+1)

fig.update_layout(
    height=rn* 600,
)
fig.show()

Hi @mithril welcome to the forums.

This might be helpful:

Thank you for the reference.
I found it very complicated to config the colorbar position automatically .

I changed trace to

cn=3
rn=int(np.ceil(len(cols)/cn))

for i, c in enumerate(cols):
    # print(i//cn+1,i%cn+1)
    
    _r = i//cn+1
    _c = i%cn+1

    trace = go.Heatmap(
                z=data, 
                x=data.columns,
                y=data.index,
                colorbar=dict(x=floor_point(_c*1/cn-0.05, 3), y=floor_point(_r*1/rn-0.05, 3), title=c, thickness=15),
                # coloraxis=f'coloraxis{i+1 if i else ""}',
            )

Stil awful :

Very frustrated

I agree, it’s a pain.

Could you create a MRE so that we can play around with this?

Sure, since this site can’t upload csv, I paste it here after_n,days,start,end,total_trades,max_dd,win_rate,total_return,benchmark_retur - Pastebin.com

# You need read the csv and set first two columns to index
s = pd.read_csv('1.csv').set_index(['after_n', 'days'])

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


import math
def floor_point(x, num):
    return math.floor(x*10**num)/10**num


cols = ['total_trades', 'max_dd', 'win_rate', 'total_return', 'sharpe_ratio', 'calmar_ratio', ]

cn=3
rn=int(np.ceil(len(cols)/cn))

pad_titles = cols +  (cn*rn - len(cols)) * ['']


fig = make_subplots(rows=rn, cols=cn, subplot_titles=pad_titles, horizontal_spacing=0.15, vertical_spacing=0.08)


for i, c in enumerate(cols):
    # print(i//cn+1,i%cn+1)
    
    _r = i//cn+1
    _c = i%cn+1
    print(_r, _c,  floor_point(_c*1/cn-0.05, 2), floor_point(_r*1/rn-0.05, 2))

    data = s[c].to_frame().unstack().droplevel(0, axis=1).T
    print(f'coloraxis{i+1 if i else ""}')
    
    trace = go.Heatmap(
                z=data, 
                x=data.columns,
                y=data.index,
                colorbar=dict(x=floor_point(_c*1/cn-0.05, 3), y=floor_point(_r*1/rn-0.05, 3), title=c, thickness=15),
            )
    

    
    fig.add_trace(trace, row=i//cn+1, col=i%cn+1)
    



fig.update_layout(
    height=rn* 600,
)
fig.show()