.png 输出每一行 data.frame 并制作 .gif 动画 [英] .png output for each row of data.frame and making .gif animation

查看:23
本文介绍了.png 输出每一行 data.frame 并制作 .gif 动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从数据框的每一行生成 .png 时遇到了一些麻烦.

I have some trouble with generating .png from each rows of data frame.

基本上,我想将 df 的每一行 rbindcoordinate_sys.

Basically, I want to rbind each one of the row of df to coordinate_sys.

对于 dfcoordinate_sys 的每一行,应该生成一个坐标系和一个单位向量J",就像 这个

For each row of df together with coordinate_sys, a coordinate system and one a unit vector "J" should be generated like in this

最后,在为每个 unit_vector 生成一个 .png 文件后,我想制作 .gif 动画.

Finally, after generating a .png file for each unit_vector, I would like to make .gif animation.

这是我的脚本的可重现代码;

here is the reproducible code of my script;

library(matlib)
library(rgl)
set.seed(12)
x <- runif(10,-0.14,0.1)
y <- runif(10,-0.14,0.1)
z <-sort(runif(10,-0.9,0.9),decreasing=TRUE)
df <- data.frame(x,y,z)


rot <- function(df,out){
  coordinate_sys <- rbind(c(1,0,0),c(0,-1,0),c(0,0,1))
  vec <- rbind(coordinate_sys, unlist(df))
  rownames(vec) <- c("X", "Y", "Z", "J")
  print(vectors3d(vec, col=c(rep("black",3), "red"), lwd=2))
 out <-  png(file="example%02d.png", width=200, height=200)
  dev.off()
}

apply(df, 1,rot,out)

推荐答案

这就是我得到的程度:

rot <- function(dat, i){
  coordinate_sys <- rbind(c(1,0,0),c(0,-1,0),c(0,0,1))
  vec <- rbind(coordinate_sys, unlist(dat))
  rownames(vec) <- c("X", "Y", "Z", "J")
  open3d()
  plot3d(1, xlim = c(-1, 1), ylim = c(-1, 1), zlim = c(-1, 1), type = 'n')
  print(vectors3d(vec, col=c(rep("black",3), "red"), lwd=2))
  rgl.snapshot(paste0(letters[i], '.png'))
  rgl.close()
}

mapply(rot, split(df, 1:nrow(df)), i = 1:nrow(df))

此代码将生成 10 个 .png 图像,每个图像具有一个向量和一个稳定的坐标系.(虽然它会产生警告.)

This code will generate 10 .png images with one vector in each and a stable coordinate system. (It generates warnings though.)

现在,我们应该能够使用动画包将其制作成 .gif,但由于 ImageMagick 不想读取 .png 文件(?),它在我的机器上失败了.也许它对你有用.

Now, we should be able to make that into a .gif using the animation package, but it fails on my machine due ImageMagick not wanting to read .png files (?). Perhaps it will work on yours.

library(animation)
im.convert('*.png')

这篇关于.png 输出每一行 data.frame 并制作 .gif 动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆