Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Keeping a consistent perspective for 3D plots

I am trying to animate a large 3D scatter plot. Because the vectors involved are large, it is too slow to render if I use plotly’s built-in animation capability, so instead, I am saving each frame as a PNG file and using another tool to do the animation.
I am having some trouble with the perspective bouncing around between frames, even though I am using the same scene (including camera “up”, “center” and “eye” and axis information) and the same x and y values for each frame. Hence, most of my frames look like this:

But for a few frames, it zooms in for no apparent reason and it looks like this:

(In case you are curious, this is simulated ocean colour and surface elevation overlaying a map from ggmap().

Is there anything I can do to stop it from zooming in and out?

Here’s my code:

eye <- list(x = 0.065, y =-1.625, z = 0.2925)
scene = list(camera = list(up = list(x=0, y=0, z=1), 
			   center=list(x=0, y=0, z=0),
			   eye = eye),
	     xaxis = list(range = c(142.1688, 157),
				    title="",
			  showline=FALSE,
			  showticklabels=FALSE,
			  showline=FALSE,
			  showgrid=FALSE),
	     yaxis = list(range = c(-28.7, -7.01),
			  title="",
			  showline=FALSE,
			  showticklabels=FALSE,
			  showline=FALSE,
			  showgrid=FALSE),
	     zaxis = list(range = c(-2.4,8),
			  title="",
			  showline=FALSE,
			  showticklabels=FALSE,
			  showline=FALSE,
			  showgrid=FALSE),
	     showlegend = FALSE
	     )

for (i in 1:dim(z)[3]) { 

p <- plot_ly(x=x,
	     y=y,
	     z=c(z[,,i]),
	     type="scatter3d",
	     mode="markers",
	     marker=list(color=c(truecolor[,,i]), size=2, alpha=0.5),
	     showlegend=FALSE) %>%
        layout(scene = scene)

p <- add_trace(p,  x=mapx, 
	       y=mapy, 
               z= - 2.4,
               type="scatter3d",
               mode="markers",
               marker=list(color=mapcolor, size=2),
               showlegend=FALSE) 

fname <- paste0('elev_movie/A', 100000 + i, '.png', collapse='')
plotly_IMAGE(p, format="png", out_file=fname)
}
1 Like

Hey there,

To prevent this behaviour set in xaxis, yaxis, zaxis, autorange=False.

1 Like

Thank you – I really appreciate the help. I’ve just tried it, but unfortunately, setting autorange=FALSE for xaxis, yaxis and zaxis didn’t make any difference. To clarify: the axes aren’t changing between images, just the perspective.

Update: I’ve partially fixed the problem by the enormous kludge of adding a “ghost” trace (a scatter3d trace with alpha=0.00005) of one of the troublesome frames to every other frame – see below for the results – but I’d still like to learn how to do this properly. I am sure there is something simple that I am missing!