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

Plotly子图与图形对象报错:scatter3d与xy子图类型不兼容

问题原因

make_subplots 默认创建的是2D(xy类型)子图网格,而你要添加的 scatter3d 是3D轨迹,两者类型不兼容,因此抛出 ValueError。

解决方案

你需要在创建子图时显式指定子图类型为3D,有两种方式:

方式1:全局设置所有子图为3D类型

直接在 make_subplots 中传入 subplot_type='3d',让所有子图都适配3D轨迹:

row_num = len(dfbh['client'].value_counts().index.tolist())
# 创建3D子图网格
fig = make_subplots(rows=row_num, cols=1, subplot_type='3d')

scatbh = go.Scatter3d(x=dfbhtemp['x_local'], y=dfbhtemp['y_local'], z=dfbhtemp['z_local'], mode='markers')
scatrun = go.Scatter3d(x=dfrunstemp['x_local'], y=dfrunstemp['y_local'], z=dfrunstemp['z_local'], mode='markers')

fig.add_trace(scatbh, row=1, col=1)
fig.add_trace(scatrun, row=1, col=1)
fig.show()

方式2:单独指定每个子图的类型(适合混合2D/3D子图场景)

如果需要部分子图为3D、部分为2D,可以用 specs 参数逐个定义子图类型:

row_num = len(dfbh['client'].value_counts().index.tolist())
# 为每一行的子图指定3D类型
fig = make_subplots(rows=row_num, cols=1, specs=[[{'type': '3d'}] for _ in range(row_num)])

scatbh = go.Scatter3d(x=dfbhtemp['x_local'], y=dfbhtemp['y_local'], z=dfbhtemp['z_local'], mode='markers')
scatrun = go.Scatter3d(x=dfrunstemp['x_local'], y=dfrunstemp['y_local'], z=dfrunstemp['z_local'], mode='markers')

fig.add_trace(scatbh, row=1, col=1)
fig.add_trace(scatrun, row=1, col=1)
fig.show()
补充说明

你提到不设置行列的子图能正常显示,是因为直接用 go.Figure() 创建的空白图,在添加 scatter3d 轨迹时会自动适配3D布局;而 make_subplots 为了兼容多子图场景,默认采用2D布局,必须显式声明3D类型才能匹配3D轨迹。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 03:10:35