# How to subplot heatmap with side bar in right position?

**URL:** <https://community.plotly.com/t/how-to-subplot-heatmap-with-side-bar-in-right-position/76114>\
**Category:** 📊 Plotly Python\
**Created:** [June 15, 2023, 6:17am UTC](https://community.plotly.com/t/how-to-subplot-heatmap-with-side-bar-in-right-position/76114 "2023-06-15T06:17:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mithril](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mithril](https://community.plotly.com/u/mithril)\
**Post date:** [June 15, 2023, 6:17am UTC](https://community.plotly.com/t/how-to-subplot-heatmap-with-side-bar-in-right-position/76114/1 "2023-06-15T06:17:29Z")

</div>

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 ?

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

```

 ![MWXF](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/9/b/9ba9726133be1b2ec81c32b43a97a413662b275b.png)

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [June 15, 2023, 7:23am UTC](https://community.plotly.com/t/how-to-subplot-heatmap-with-side-bar-in-right-position/76114/2 "2023-06-15T07:23:11Z")

</div>

Hi @mithril welcome to the forums.

This might be helpful:

> [@Colorbar for each facet\_col](https://community.plotly.com/t/colorbar-for-each-facet-col/73456):
>
> how can I get colorbar for each facet\_col? currently I am getting only one colorbar common for all facetcol.

---

<div class="post-metadata">

**Author:** ![mithril](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mithril](https://community.plotly.com/u/mithril)\
**Post date:** [June 15, 2023, 8:11am UTC](https://community.plotly.com/t/how-to-subplot-heatmap-with-side-bar-in-right-position/76114/3 "2023-06-15T08:11:39Z")

</div>

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

I changed trace to

```auto
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 :

 ![QQ图片20230615161001](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/9/6/962792cef136576027b3e2689d89a1ad8a92f4d3.png)

Very frustrated

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [June 15, 2023, 8:16am UTC](https://community.plotly.com/t/how-to-subplot-heatmap-with-side-bar-in-right-position/76114/4 "2023-06-15T08:16:31Z")

</div>

I agree, it’s a pain.

Could you create a [MRE](https://community.plotly.com/t/how-to-write-a-minimal-reproducible-example-mre/61502) so that we can play around with this?

---

<div class="post-metadata">

**Author:** ![mithril](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mithril](https://community.plotly.com/u/mithril)\
**Post date:** [June 15, 2023, 8:31am UTC](https://community.plotly.com/t/how-to-subplot-heatmap-with-side-bar-in-right-position/76114/5 "2023-06-15T08:31:08Z")

</div>

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](https://pastebin.com/iE6VDqkU)

```auto
# 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()

```
