从 plotly 3d 图表创建视频
create a video from plotly 3d chart
我想知道是否有人知道将 plotly 3d 图表导出为视频的方法(更具体地说,如果这可以在本机完成或需要 bodging)?
导出静态图像很简单,导出交互式图可以很好地嵌入 HTML 等
假设我有一个我想要的 3D 图表,所以只需缓慢旋转,如果图像可以旋转给定的间隔,拍摄的图像,进一步旋转,这看起来可能非常简单 ad infinitum,也许在一个循环中 - 但我想知道这是否不受本地支持?
有人知道好的策略吗?
R/RStudio 的理想解决方案,但由于 plotly 是跨平台的,因此考虑了任何解决方案。
供日后参考:
能够迭代多个视角的关键在于相机控制“眼睛”,plotly help center 向我指出了这一点:
https://plot.ly/r/reference/#layout-scene-camera
camera = list(eye = list(x = 1.25, y = 1.25, z = 1.25))) #1.25 is default
虽然在上面搜索我的特定查询没有找到它,但在这里得到了某种程度的回答:
我在脚本中使用了一个 for 循环将迭代器传递给一个三角函数,该函数为相机坐标绘制一个圆,在每一步渲染一个新图像。
(x,y) = cos(theta) + sin(theta)
最终代码如下所示:
# assume dataset read in, manipulated and given as a matrix in "matrix"
matrix.list <- list(x = temp, y = scan, z = matrix)
font.pref <- list(size=12, family="Arial, sans-serif", color="black")
x.list <- list(title = "X", titlefont = font.pref)
y.list <- list(title = "Y", titlefont = font.pref)
z.list <- list(title = "Z",titlefont = font.pref)
zoom <- 2
for(i in seq(0,6.3,by=0.1){
# 6.3 is enough for a full 360 rotation
outfile <- paste(file,"plot",i, sep = "_")
graph <- plot_ly(matrix.list, x = temp, y = scan, z = z,
type="surface") %>%
layout(scene=list(xaxis = x.list,
yaxis = y.list,
zaxis = z.list,
camera = list(eye = list(x = cos(i)*zoom, y = sin(i)*zoom, z= 0.25))))
# The above camera parameters should orbit
# horizontally around the chart.
# The multiplier controls how far out from
# from the graph centre the camera is (so
# is functionally a 'zoom' control).
graph
plotly_IMAGE(graph, username="xxx", key="xxx",
out_file = paste(outfile,"png", sep="."))
}
NB 文件数量及其分辨率最终可能会占用大量 space.
注意 2 我在构造这个时忘记了 plotly 的免费 API 限制你每天只能调用 50 API 次,所以如果你想渲染一个视频,相应地调整你的框架等......
我想知道是否有人知道将 plotly 3d 图表导出为视频的方法(更具体地说,如果这可以在本机完成或需要 bodging)?
导出静态图像很简单,导出交互式图可以很好地嵌入 HTML 等
假设我有一个我想要的 3D 图表,所以只需缓慢旋转,如果图像可以旋转给定的间隔,拍摄的图像,进一步旋转,这看起来可能非常简单 ad infinitum,也许在一个循环中 - 但我想知道这是否不受本地支持?
有人知道好的策略吗?
R/RStudio 的理想解决方案,但由于 plotly 是跨平台的,因此考虑了任何解决方案。
供日后参考:
能够迭代多个视角的关键在于相机控制“眼睛”,plotly help center 向我指出了这一点:
https://plot.ly/r/reference/#layout-scene-camera
camera = list(eye = list(x = 1.25, y = 1.25, z = 1.25))) #1.25 is default
虽然在上面搜索我的特定查询没有找到它,但在这里得到了某种程度的回答:
我在脚本中使用了一个 for 循环将迭代器传递给一个三角函数,该函数为相机坐标绘制一个圆,在每一步渲染一个新图像。
(x,y) = cos(theta) + sin(theta)
最终代码如下所示:
# assume dataset read in, manipulated and given as a matrix in "matrix"
matrix.list <- list(x = temp, y = scan, z = matrix)
font.pref <- list(size=12, family="Arial, sans-serif", color="black")
x.list <- list(title = "X", titlefont = font.pref)
y.list <- list(title = "Y", titlefont = font.pref)
z.list <- list(title = "Z",titlefont = font.pref)
zoom <- 2
for(i in seq(0,6.3,by=0.1){
# 6.3 is enough for a full 360 rotation
outfile <- paste(file,"plot",i, sep = "_")
graph <- plot_ly(matrix.list, x = temp, y = scan, z = z,
type="surface") %>%
layout(scene=list(xaxis = x.list,
yaxis = y.list,
zaxis = z.list,
camera = list(eye = list(x = cos(i)*zoom, y = sin(i)*zoom, z= 0.25))))
# The above camera parameters should orbit
# horizontally around the chart.
# The multiplier controls how far out from
# from the graph centre the camera is (so
# is functionally a 'zoom' control).
graph
plotly_IMAGE(graph, username="xxx", key="xxx",
out_file = paste(outfile,"png", sep="."))
}
NB 文件数量及其分辨率最终可能会占用大量 space.
注意 2 我在构造这个时忘记了 plotly 的免费 API 限制你每天只能调用 50 API 次,所以如果你想渲染一个视频,相应地调整你的框架等......