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

如何用R语言rgl包绘制两点间连线?lines3d无响应问题排查

问题原因及解决方法

你的代码里lines3d调用存在两处错误:

  • 参数格式错误:lines3d需要接收每个坐标轴的点序列(比如x轴的所有点坐标、y轴的所有点坐标、z轴的所有点坐标),而非直接传入两个单独的点向量。你需要把from和to的对应坐标分别组合成向量,或者将两个点合并为一个2行3列的矩阵传入。
  • 参数名称错误:控制线条宽度的参数是lwd,不是size——size是给points3d设置点大小的参数,对lines3d无效。

修正后的代码

library(MASS)
library(rgl)
set.seed(77)

## means
mu <- c(1, 3, 2)
## covariance matrix
Sigma <- matrix(c(3.0, 1.5, 1.0, 
                  1.5, 2.0, 0.5, 
                  1.0, 0.5, 1.0), 
                nrow = 3, byrow = TRUE)
## sample
d3 <- mvrnorm(n = 500, mu = mu, Sigma = Sigma)

from <- c(1, 3, 2)
to <- c(2, 2, 1)

par3d(windowRect = c(819, 88, 1492, 706))  
plot3d(d3[, 1], d3[, 2], d3[, 3], col = "blue", size = 5,
       xlab = "x", ylab = "y", zlab = "z",
       aspect = c(1, 1, 1))
points3d(from[1], from[2], from[3], col = "red", size = 10)
points3d(to[1], to[2], to[3], col = "red", size = 10)
# 方法1:分别传入x、y、z的坐标序列
lines3d(x = c(from[1], to[1]), 
        y = c(from[2], to[2]), 
        z = c(from[3], to[3]), 
        color = "red", lwd = 3)

# 或者方法2:将两个点合并为矩阵传入
# lines3d(rbind(from, to), color = "red", lwd = 3)

修改后,两点间的红色连线即可正常显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 02:57:27