【问题标题】:3D scatter plot in real time实时 3D 散点图
【发布时间】:2014-10-15 16:35:43
【问题描述】:

我在 R 中工作并拥有以下形式的数据。一个数据框,其中第一列是一个人的出生日期,第二列是年龄,第三列是死因(自然/疾病/事故),第四列是种族,第五列是出生时的体重,第六列是出生时的长度。

最后,我想创建一个 3D 图形的短视频/时间流逝,最后三列作为空间维度。视频播放时间对应于收集我的数据的总时间。视频播放时,点会以黑点的形式出现在相应的 3D 位置,然后根据其死因变为蓝色/绿色/红色。

在追求这个想法的过程中,我遇到了this,但它似乎实时移动了轴,而不是实时填充图表。我担心这从根本上来说是一项不同的任务。我还看到 jfreechart.com 对此很有用,但如果可能的话,我更喜欢在 R/Matlab 中完成此操作,然后再求助于其他软件。最后,我愿意使用/学习完成此任务所需的任何软件。

感谢您抽出宝贵时间!

【问题讨论】:

  • Matlab 中绝对可以执行视频中显​​示的操作(虽然我对R 不太熟悉,但我相信这也是可行的)。无需为这项任务学习一门新语言。您应该尝试使用您最熟悉的语言开始尝试,然后在此处发布您的结果,然后社区可以帮助您完善它。

标签: r matlab plot real-time


【解决方案1】:

这可以使用 R 包 scatterplot3danimation 来完成。后者需要安装ImageMagick

这里有一些代码可以完成你想做的事情。

require(animation)
require(scatterplot3d)

# Get some example data
n <- 10
dt <- data.frame(birth = rnorm(n, 50, 20),
                 age = sample(1:100, n, replace=TRUE), 
                 cause = sample(1:3, n, replace=TRUE), 
                 race = sample(1:5, n, replace=TRUE),
                 bweight = rnorm(n, 1000, 200),
                 blen = rnorm(n, 300, 20))

# Starting and final timepoint
st <- 1
ft <- 150

# All the timepoints to evaluate
times <- st:ft

# Matrices that show for each timepoint whether a person is alive or dead.
born <- outer(dt$birth, times, "<")
dead <- outer(dt$birth + dt$age, times, "<")

# Matrix is 0 for not yet born, 1 for living, 2 for dead.
status <- born + dead

# If dead, this contains the status of the cause.
deadcause <- matrix(dt$cause, nc=length(times), nrow=n) * dead + born

# Function to get animations
anim <- function(dt, times) {
  for(i in seq_along(times)) {

    # Remove those not yet born.
    dtcur <- dt * born[, i]

    scatterplot3d(x=dtcur$race, 
                  y=dtcur$bweight, 
                  z=dtcur$blen, 
                  color=deadcause[, i],
                  angle = 135, type = "h",
                  main=paste("At time", i),
                  xlim=c(0,5), 
                  ylim=c(0,1500), 
                  zlim=c(0,500))
    animation::ani.pause()
  }
}

# Save the gif to current directory.
saveGIF(expr = anim(dt, times), interval=.1, outdir = getwd())

【讨论】:

    猜你喜欢
    • 2017-02-11
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多