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

Plotly Python:如何让子图中的3D图表完整显示无截断

解决Plotly 1行3列3D子图坐标轴截断问题

当使用Plotly创建1行3列的3D子图时,默认布局的宽高不足以容纳三个3D场景,会导致坐标轴被截断。以下是针对你的代码的修复方案:

修改后的完整代码

import pandas as pd
import plotly.io as pio
pio.renderers.default='browser'
from plotly.subplots import make_subplots
import plotly.graph_objects as go

d = {'Temp1':  [25, 25, 25, 210, 210, 210], 
     'Dopant': [3, 6, 3, 6, 3, 6],
     'Temp2':  [1.5, 9, 15, 30, 60, 120], 
     'Strain': [0.6, 1.3, 1.6, 2.8, 4.3, 6.9],
     'Stress': [2400, 2900, 2950, 3100, 3060, 2700], 
     'Dcut':   [7.7, 6.2, 5.7, 4.9, 4.2, 3.6],     }
df = pd.DataFrame(d)

fig = make_subplots( rows=1, cols=3, 
                     specs=[[{'type': 'scene'}, {'type': 'scene'}, 
                             {'type': 'scene'}]] )

x1v = df[ 'Temp1' ].values
x2v = df[ 'Dopant' ].values
x3v = df[ 'Temp2' ].values
y1v = df[ 'Stress' ].values
y2v = df[ 'Strain' ].values
y3v = df[ 'Dcut' ].values

fig.add_trace( go.Scatter3d( x=x1v, y=x2v, z=x3v, 
                            mode='markers',
                            marker=dict(color=y1v,
                                        colorscale='Viridis' ) ), 
                   row=1, col=1 )

fig.add_trace( go.Scatter3d( x=x1v, y=x2v, z=x3v, 
                            mode='markers',
                            marker=dict(color=y2v,
                                        colorscale='Viridis' ) ), 
                   row=1, col=2 )

fig.add_trace( go.Scatter3d( x=x1v, y=x2v, z=x3v, 
                            mode='markers',
                            marker=dict(color=y3v,
                                        colorscale='Viridis' ) ), 
                   row=1, col=3 )

# 核心修复设置
fig.update_layout(
    height=800,  # 调整高度,保证3D场景有足够垂直空间
    width=1800,  # 大幅增加宽度,容纳三个并排的3D子图
    margin=dict(l=50, r=50, t=50, b=50)  # 调整边距,避免边缘内容被截断
)

# 统一设置所有3D场景的坐标轴比例和显示
fig.update_scenes(
    aspectmode='data',  # 让坐标轴比例适配数据范围,避免挤压
    xaxis_title='Temp1',
    yaxis_title='Dopant',
    zaxis_title='Temp2'
)

fig.show()

关键修复点说明

  • 增大整体宽高:原代码默认宽高太小,三个3D子图横向排列时空间不足,通过设置width=1800和height=800给每个子图足够的展示空间。
  • 调整边距:margin参数设置避免图表边缘的坐标轴标签或刻度被截断。
  • 设置场景坐标轴比例:aspectmode='data'让每个3D场景的坐标轴比例根据数据的实际范围自动调整,不会强行压缩成正方体,保证数据展示的合理性。如果需要正方体样式,可以将aspectmode改为'cube'。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 23:45:42