Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share the same Z axis on 2 plotly Heatmap subplots

I would like to set the same Z axis for 2 heatmap subplots. Currently, plotly generates 2 independents scale and superimposes them.

Superimposed scaled

Here is where I am for the moment, but cannot manage to merge both scales...

import plotly.offline as py
import plotly.graph_objs as go
from plotly import tools

fig = tools.make_subplots(rows=1, cols=2, shared_yaxes=True)
# shared_zaxes=True does not exists
data = [
        {'x' : [1, 2, 3],
         'y' : [1, 2, 3],
         'z' : [[2,5,6],[8,7,3],[1,7,3]]
         },
        {'x' : [1, 2, 3],
         'y' : [1, 2, 3],
         'z' : [[1,2,3],[2,3,1],[1,2,3]]
        }
        ]
maxValue = 0

for i in [0,1]:
    print(i)
    trace = go.Heatmap(z=data[i]['z'],
                       x=data[i]['x'],
                       y=data[i]['y'],
                       colorscale='Jet')
    fig.append_trace(dict(trace, showscale=True), 1, i+1) 

    maxValue = max(max(data[i]['z'])) if max(max(data[i]['z'])) > maxValue else maxValue

fig['layout'].update(title='Same Zaxis')
fig['layout']['scene']['zaxis'].update(range=[0,maxValue])

py.plot(fig , filename='sameZaxis-heatmap')
like image 928
Maxime Avatar asked Sep 18 '25 10:09

Maxime


1 Answers

I finally simply compute the max value of the scale before looping, and then apply it to both graphs. Then, both scales are superimposed but you cannot notice it.

#Define the max value of you scale before looping:
maxValue = ...

for i in [0,1]:
    trace = go.Heatmap(...
                       zmin=0,
                       zmax=maxValue))
like image 82
Maxime Avatar answered Sep 19 '25 23:09

Maxime