You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly多3D子图Z轴缩放不一致问题求助

问题

我用Plotly编写代码生成包含4个3D子图的HTML文件,单独生成每个子图时Z轴能按要求缩放到0到-5,但合并成子图后只有第一个子图符合缩放要求,其余子图均自动使用数据自身的最大最小值,显示效果夸张。

代码如下:

"""Plot 3D streamer surfaces."""
# Initialise figure with subplots
fig4S = make_subplots(rows=2, cols=2, specs=[[{'is_3d': True},
                      {'is_3d': True}], [{'is_3d': True},
                                         {'is_3d': True}]],
                      subplot_titles=("Streamer 1",
                      "Streamer 2", "Streamer 3", "Streamer 4"),
                      shared_xaxes=False, row_heights=[0.5, 0.5],
                      vertical_spacing=0.05)
zS1 = 0-dfS1  # Depth data for each surface, made negative as it's a depth below sea surface
zS2 = 0-dfS2
zS3 = 0-dfS3
zS4 = 0-dfS4
fig4S.add_trace(go.Surface(z=zS1, cmin=-5, cmax=0,
                colorscale=[[0, 'violet'], [0.2, 'blue'],
                            [0.35, 'lightblue'], [0.50, 'green'],
                            [0.65, 'yellow'], [0.8, 'orange'],
                            [1, 'red']]), 1, 1)
fig4S.add_trace(go.Surface(z=zS2, cmin=-5, cmax=0,
                colorscale=[[0, 'violet'], [0.2, 'blue'],
                            [0.35, 'lightblue'], [0.50, 'green'],
                            [0.65, 'yellow'], [0.8, 'orange'],
                            [1, 'red']]), 1, 2)
fig4S.add_trace(go.Surface(z=zS3, cmin=-5, cmax=0,
                colorscale=[[0, 'violet'], [0.2, 'blue'],
                            [0.35, 'lightblue'], [0.50, 'green'],
                            [0.65, 'yellow'], [0.8, 'orange'],
                            [1, 'red']]), 2, 1)
fig4S.add_trace(go.Surface(z=zS4, cmin=-5, cmax=0,
                colorscale=[[0, 'violet'], [0.2, 'blue'],
                            [0.35, 'lightblue'], [0.50, 'green'],
                            [0.65, 'yellow'], [0.8, 'orange'],
                            [1, 'red']]), 2, 2)

fig4S.update_traces(contours_z=dict(show=True, usecolormap=True,
                                    highlightcolor="limegreen"))
fig4S.update_scenes(aspectratio=dict(x=2, y=2, z=0.5))
fig4S.update_layout(scene=dict(zaxis=dict(nticks=4, range=[-5, 0])))
fig4S.update_layout(template='plotly_dark',
                    title="Channel Depths    Line: " +
                    str(name)+"  Seq: "+str(Seq),
                    xaxis=dict(automargin=True))
fig4S.write_html("C:/Users/client/Desktop/4_Streamer_Depths.html")

数据示例:

R1d R2d R3d R4d R5d R6d R7d
0   2.7 2.6 2.4 2.4 2.4 2.4 2.4
1   2.7 2.6 2.4 2.4 2.4 2.4 2.4
2   2.8 2.6 2.4 2.4 2.4 2.4 2.4
3   2.8 2.6 2.4 2.4 2.4 2.4 2.4
4   2.8 2.6 2.4 2.4 2.4 2.4 2.4
5   2.8 2.6 2.5 2.5 2.4 2.4 2.4
6   2.8 2.6 2.5 2.5 2.5 2.4 2.4
7   2.8 2.6 2.5 2.5 2.4 2.4 2.4
8   2.8 2.6 2.5 2.5 2.4 2.4 2.4
9   2.8 2.6 2.5 2.4 2.4 2.4 2.3
解决方案

问题出在update_layout(scene=...)仅对第一个3D子图生效。Plotly中每个3D子图对应独立的scene对象,第一个子图默认绑定scene,后续子图依次绑定scene2、scene3、scene4,仅设置scene不会影响其他子图,导致它们自动适配数据的Z轴范围。

推荐用update_scenes统一设置所有子图的Z轴范围,简洁高效。修改原代码中的:

fig4S.update_layout(scene=dict(zaxis=dict(nticks=4, range=[-5, 0])))

替换为:

fig4S.update_scenes(zaxis=dict(nticks=4, range=[-5, 0]))

update_scenes会对所有3D子图的场景应用相同设置,确保所有子图的Z轴都缩放到0到-5范围。

完整修改后的代码:

"""Plot 3D streamer surfaces."""
# Initialise figure with subplots
fig4S = make_subplots(rows=2, cols=2, specs=[[{'is_3d': True},
                      {'is_3d': True}], [{'is_3d': True},
                                         {'is_3d': True}]],
                      subplot_titles=("Streamer 1",
                      "Streamer 2", "Streamer 3", "Streamer 4"),
                      shared_xaxes=False, row_heights=[0.5, 0.5],
                      vertical_spacing=0.05)
zS1 = 0-dfS1  # Depth data for each surface, made negative as it's a depth below sea surface
zS2 = 0-dfS2
zS3 = 0-dfS3
zS4 = 0-dfS4
fig4S.add_trace(go.Surface(z=zS1, cmin=-5, cmax=0,
                colorscale=[[0, 'violet'], [0.2, 'blue'],
                            [0.35, 'lightblue'], [0.50, 'green'],
                            [0.65, 'yellow'], [0.8, 'orange'],
                            [1, 'red']]), 1, 1)
fig4S.add_trace(go.Surface(z=zS2, cmin=-5, cmax=0,
                colorscale=[[0, 'violet'], [0.2, 'blue'],
                            [0.35, 'lightblue'], [0.50, 'green'],
                            [0.65, 'yellow'], [0.8, 'orange'],
                            [1, 'red']]), 1, 2)
fig4S.add_trace(go.Surface(z=zS3, cmin=-5, cmax=0,
                colorscale=[[0, 'violet'], [0.2, 'blue'],
                            [0.35, 'lightblue'], [0.50, 'green'],
                            [0.65, 'yellow'], [0.8, 'orange'],
                            [1, 'red']]), 2, 1)
fig4S.add_trace(go.Surface(z=zS4, cmin=-5, cmax=0,
                colorscale=[[0, 'violet'], [0.2, 'blue'],
                            [0.35, 'lightblue'], [0.50, 'green'],
                            [0.65, 'yellow'], [0.8, 'orange'],
                            [1, 'red']]), 2, 2)

fig4S.update_traces(contours_z=dict(show=True, usecolormap=True,
                                    highlightcolor="limegreen"))
fig4S.update_scenes(aspectratio=dict(x=2, y=2, z=0.5))
# 统一设置所有子图的Z轴范围
fig4S.update_scenes(zaxis=dict(nticks=4, range=[-5, 0]))
fig4S.update_layout(template='plotly_dark',
                    title="Channel Depths    Line: " +
                    str(name)+"  Seq: "+str(Seq),
                    xaxis=dict(automargin=True))
fig4S.write_html("C:/Users/client/Desktop/4_Streamer_Depths.html")

内容的提问来源于stack exchange,提问作者WillH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 05:27:57