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

如何使用R语言plotly库在同一画布绘制3D曲面与数据散点

R语言Plotly叠加3D散点与椭球曲面的方法

你可以通过Plotly的图层叠加能力直接将两类元素放在同一张3D画布上,具体实现如下:

首先修正你现有代码的小问题:代码中Library(rgl)首字母大写,R大小写敏感,需要改为library(rgl)才能正常加载依赖包。

方法1:管道符追加图层(推荐)

直接在散点图对象上追加椭球的mesh3d trace,逻辑更简洁:

library(plotrix)
library(plotly)
library(Rvcg)
library(rgl)

# 读取数据生成散点图
df_r_n_r = read.csv('temp.csv', header = TRUE)

# 计算椭球参数
D <- rbind(c(459.956, -34.198, -29.844), c(-34.198, 481.647, 1.505), c(-29.844, 1.505, 559.393))
A <- D %*% t(D)
Am <- solve(A)
o <- c(73.658, 420.551, -429.058)
r <- 1

sphr <- vcgSphere()
ell <- scale3d(transform3d(sphr, chol(Am)), r, r, r)
vs <- ell$vb[1:3,] + o
idx <- ell$it - 1

# 直接叠加生成组合图,额外设置了区分色方便观察
combined_plot <- plot_ly(df_r_n_r,type = "scatter3d", mode="markers", x = ~x, y = ~y, z = ~z, color = I("darkblue")) %>%
  add_trace(type="mesh3d",
    x = vs[1,], y = vs[2,], z = vs[3,],
    i = idx[1,], j = idx[2,], k = idx[3,],
    opacity = 0.3, color = I("orange"))

# 查看结果
combined_plot

方法2:合并已有的两个独立绘图对象

如果你已经提前生成了独立的fig(散点图)和e_plot(椭球图)对象,使用subplot函数开启三轴共享即可对齐坐标:

combined_plot <- subplot(fig, e_plot, shareX = TRUE, shareY = TRUE, shareZ = TRUE)
combined_plot

以上两种方法都可以实现散点和椭球的同框展示,方便你直观验证拟合效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 06:03:03