Fig.update_layout is only valid for the first subplot

Hi,

I’m trying to have multiple subplots with my defined sense. However, it seems fig.update_layout only works for my first subplot.

Just made a basic 2 subplot:

# Define frames
import plotly.graph_objects as go
from plotly.subplots import make_subplots

def draw_volumes(volumes, x_slice, y_slice, z_slice, cmap='gray'):
    
    fig = make_subplots(
        rows=1, cols=2,
        specs=[[{'type': 'surface'}, {'type': 'surface'}]])
    rr, cc = [1,1], [1,2]

    for i,volume in enumerate(volumes):

        r,c = rr[i], cc[i]
        z, y, x = volume.shape
        cmin=np.min(volume)
        cmax=np.max(volume)

        # x-slice
        yy = np.arange(0, y, 1)
        zz = np.arange(0, z, 1)
        yy,zz = np.meshgrid(yy,zz)
        xx = x_slice * np.ones((y, z)).T
        vv = volume[:,:,x_slice]
        fig.add_trace(go.Surface(
            z=zz,
            x=xx,
            y=yy,
            surfacecolor=vv,
            colorscale=cmap,
            cmin=cmin, cmax=cmax,
            showscale=False),
            row=r, col=c)

        # y-slice
        xx = np.arange(0, x, 1)
        zz = np.arange(0, z, 1)
        xx,zz = np.meshgrid(xx,zz)
        yy = y_slice * np.ones((x, z)).T
        vv = volume[:,y_slice,:]
        fig.add_trace(go.Surface(
            z=zz,
            x=xx,
            y=yy,
            surfacecolor=vv,
            colorscale=cmap,
            cmin=cmin, cmax=cmax,
            showscale=False),
            row=r, col=c)

        # z-slice
        xx = np.arange(0, x, 1)
        yy = np.arange(0, y, 1)
        xx,yy = np.meshgrid(xx,yy)
        zz = z_slice * np.ones((x, y)).T
        vv = volume[z_slice,:,:]
        fig.add_trace(go.Surface(
            z=zz,
            x=xx,
            y=yy,
            surfacecolor=vv,
            colorscale=cmap,
            cmin=cmin, cmax=cmax,
            showscale=False),
            row=r, col=c)
    fig.update_layout(
        scene = {
        "xaxis": {"nticks": 5},
        "yaxis": {"nticks": 5},
        "zaxis": {"nticks": 5, "autorange":'reversed'},
        'camera_eye': {"x": 1.25, "y": 1.25, "z": 1.25},
        'camera_up': {"x": 0, "y": 0, "z": 1},
        "aspectratio": {"x": 1, "y": 1, "z": 1}
            })
    fig.update_layout(
            title_text='3D subplots with different colorscales',
            height=400,
            width=1000,
            margin=dict(t=0, l=0, b=0))
    fig.show()
    
draw_volumes(volumes, x_slice, y_slice, z_slice)

@zfbi
To update layout scenes globally use fig.update_scenes(...). Example:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
fig = make_subplots(
        rows=1, cols=2,
        specs=[[{'type': 'surface'}, {'type': 'surface'}]])

fig.add_trace(go.Surface(z=np.random.rand(25).reshape((5,5))), 1,1)
fig.add_trace(go.Surface(z=np.random.rand(25).reshape((5,5)), showscale=False), 1,2)
fig.update_scenes(xaxis_nticks= 5, yaxis_nticks= 5, zaxis_nticks= 5, zaxis_autorange="reversed") 

Then

print(fig.layout)  

to inspect the new layout.
These scene settings can be omitted because they are default settings:

camera_eye=dict(x=1.25, y=1.25, z=1.25)
camera_up=dict(...)
aspectratio=dict(x=1, y=1, z=1)
1 Like