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

Plotly折线上方绘制曲面异常问题排查

问题

我用Python+Plotly绘制多条3D折线,想在折线上方绘制贴合的曲面,但折线显示正常,曲面位置错误且无法贴合折线,效果如图所示。

代码如下:

import plotly.graph_objects as go
import plotly.io as pio
import math
import numpy as np

t_changes = np.linspace((0.005*2*math.pi)/60 , (1*2*math.pi)/60 , 10)

omega_all = np.linspace((1*2*math.pi)/60  ,(12*2*math.pi)/60 , 10)

max_values_all = [
    [1.4185558393536863, 1.4325906947905434, 1.438658017942095, 1.4806644284478363, 1.4761508670671415, 1.4649492471829895, 1.464546919691028, 1.4578007061068432, 1.4428714932707587, 1.4500144702965356],
    [1.6441629388568684, 1.642323391267553, 1.6491736867101752, 1.6412988007431675, 1.6290382708032518, 1.687033971399921, 1.7249970205847203, 1.7375113332368095, 1.734492370556642, 1.7242648069286037],
    [2.0845437496335575, 2.072843683096361, 2.035169117911497, 2.063686038710132, 1.966491883139673, 2.036435788844538, 2.000384950132592, 1.9602627462709168, 1.9992717869448466, 2.02862099663119],
    [3.3253818185735087, 3.1215539630481604, 2.981889203258018, 2.8546336239840215, 2.7042612422101002, 2.575780911605224, 2.5755048740741917, 2.6205430458363095, 2.576086019821094, 2.487398700065042],
    [15.432838261722877, 6.5095720712941985, 5.200282346853118, 4.495357558732455, 4.136530871191216, 3.766634997051916, 3.6827888643956186, 3.4764385554542914, 3.430120257752737, 3.4201713261825426],
    [75.66433489618231, 15.94566966995464, 10.161220922127605, 8.007523032946853, 6.800634588849489, 6.000935050965966, 5.536898133810305, 5.21365607068837, 5.010566351336815, 4.708208983726101],
    [75.66286832418304, 15.363231967657855, 15.81080564424909, 13.0105751921456, 11.001714824633169, 9.553264617238883, 8.572992551192133, 8.004222045653602, 7.365317024063387, 7.085229662879642],
    [75.66242236630372, 15.363226081588929, 10.955988755682252, 13.158249063867139, 14.179122828320526, 13.460821531609223, 12.624633192306533, 11.937231489633758, 11.274180241228226, 10.672516546418693],
    [75.65799160190585, 21.62711751806979, 18.008379194730093, 14.53079025616018, 8.520673195597784, 14.016979085011064, 16.276408102947748, 17.05937073631494, 17.220381393056392, 16.8222819511244],
    [118.96662233164312, 66.31352570490306, 68.16962905085047, 61.396954421293266, 74.95559072827379, 67.92902322683065, 63.00643530404031, 63.21829273940191, 65.26976962802642, 67.46680681352494]
]
fig = go.Figure()

# Loop through each line and add it to the figure
for i in range(len(omega_all)):
    x_vals = [omega_all[i]*60/(2*math.pi)]* len(t_changes)
    y_vals = t_changes
    z_vals = max_values_all[i]
    fig.add_trace(go.Scatter3d(x=x_vals, y=y_vals, z=z_vals, mode='lines'))


fig.add_trace(go.Surface(x=(omega_all*60/(2*math.pi)), y=t_changes, z=max_values_all))
# Set layout
fig.update_layout(scene=dict(aspectmode="cube"))

# Show the figure
fig.show()

异常效果


原因分析与解决方案

核心问题

  1. Surface坐标轴维度不匹配:Plotly的Surface组件要求x、y数组的维度需与z数组(10×10二维数组)完全匹配,或能被广播成同维度。你传入的x和y都是一维数组,导致Plotly默认的映射逻辑错误,曲面错位。
  2. 坐标映射逻辑不一致:折线的逻辑是固定x(对应单个omega值)、遍历y(t_changes)、取对应行的z值;但默认的Surface会将z[i][j]对应x[j]和y[i],与折线的映射逻辑完全相反。

修正步骤

  • 将x转换为二维数组,每行对应一个固定的omega值,与折线的x逻辑对齐。
  • 将y转换为二维数组,每列对应一个t_changes值,保证z值的映射逻辑与折线一致。

修正后的代码

import plotly.graph_objects as go
import math
import numpy as np

t_changes = np.linspace((0.005*2*math.pi)/60 , (1*2*math.pi)/60 , 10)
omega_all = np.linspace((1*2*math.pi)/60  ,(12*2*math.pi)/60 , 10)

max_values_all = [
    [1.4185558393536863, 1.4325906947905434, 1.438658017942095, 1.4806644284478363, 1.4761508670671415, 1.4649492471829895, 1.464546919691028, 1.4578007061068432, 1.4428714932707587, 1.4500144702965356],
    [1.6441629388568684, 1.642323391267553, 1.6491736867101752, 1.6412988007431675, 1.6290382708032518, 1.687033971399921, 1.7249970205847203, 1.7375113332368095, 1.734492370556642, 1.7242648069286037],
    [2.0845437496335575, 2.072843683096361, 2.035169117911497, 2.063686038710132, 1.966491883139673, 2.036435788844538, 2.000384950132592, 1.9602627462709168, 1.9992717869448466, 2.02862099663119],
    [3.3253818185735087, 3.1215539630481604, 2.981889203258018, 2.8546336239840215, 2.7042612422101002, 2.575780911605224, 2.5755048740741917, 2.6205430458363095, 2.576086019821094, 2.487398700065042],
    [15.432838261722877, 6.5095720712941985, 5.200282346853118, 4.495357558732455, 4.136530871191216, 3.766634997051916, 3.6827888643956186, 3.4764385554542914, 3.430120257752737, 3.4201713261825426],
    [75.66433489618231, 15.94566966995464, 10.161220922127605, 8.007523032946853, 6.800634588849489, 6.000935050965966, 5.536898133810305, 5.21365607068837, 5.010566351336815, 4.708208983726101],
    [75.66286832418304, 15.363231967657855, 15.81080564424909, 13.0105751921456, 11.001714824633169, 9.553264617238883, 8.572992551192133, 8.004222045653602, 7.365317024063387, 7.085229662879642],
    [75.66242236630372, 15.363226081588929, 10.955988755682252, 13.15824906
相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 10:14:02