简体   繁体   中英

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.

叠加缩放

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')

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM