Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly : Heatmap color legend i subplot

I'm working in python with plotly, trying to have two heatmap subplot with a custom legend for each. But here what I see with the following code :

import plotly


fig = plotly.subplots.make_subplots(rows=2, cols=1)

mat = np.array([[1,0],
                [0,1]])


fig.add_trace(go.Heatmap(z=mat, colorscale='Bluered_r'), row=1, col=1)

fig.add_trace(go.Heatmap(z=mat*10, colorscale='Bluered_r'), row=2, col=1)

fig.show()

enter image description here

you can see that both legend are displayed at the same place.

Thanks for your help!

like image 546
hulyce Avatar asked Mar 16 '26 19:03

hulyce


1 Answers

Just found a solution hidden in the doc using the colorbar argument:

import plotly


fig = plotly.subplots.make_subplots(rows=2, cols=1)

mat = np.array([[1,0],
                [0,1]])


fig.add_trace(go.Heatmap(z=mat, colorscale='Bluered_r',colorbar=dict(y=.8,len=.5)), row=1, col=1)

fig.add_trace(go.Heatmap(z=mat*10, colorscale='Bluered_r',colorbar=dict(y=.2,len=.5)), row=2, col=1)

fig.show()

This would give:

enter image description here

like image 72
hulyce Avatar answered Mar 18 '26 08:03

hulyce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!