How to animate a rotation of a 3D plot?


#1

I’m trying to recreate what this guy did here:

https://codepen.io/etpinard/pen/mBVVyE?editors=0010

but in python.

Is it possible? So far I only found one resource that even mentioned rotation animation (this one), but the issue I have with it is that the anymation is either not smooth or too slow, when fiddling around with the parameters.


#2

@wtfzambo Another option is to define an animated rotation via frames (see the offline animations here: https://plot.ly/python/animations/.

Rotation function:

def rotate_z(x, y, z, theta):
    w = x+1j*y
    return np.real(np.exp(1j*theta)*w), np.imag(np.exp(1j*theta)*w), z

Frame definition:

frames=[]
for t in np.arange(0, 6.26, 0.1):
    xe, ye, ze = rotate_z(x_eye, y_eye, z_eye, -t)
    frames.append(dict(layout=dict(scene=dict(camera=dict(eye=dict(x=xe, y=ye, z=ze))))))

and updatemenus for Play button:

updatemenus=[dict(type='buttons',
                  showactive=False,
                  y=1,
                  x=0.8,
                  xanchor='left',
                  yanchor='bottom',
                  pad=dict(t=45, r=10),
                  buttons=[dict(label='Play',
                                 method='animate',
                                 args=[None, dict(frame=dict(duration=50, redraw=False), 
                                                             transition=dict(duration=0),
                                                             fromcurrent=True,
                                                             mode='immediate'
                                                            )]
                                            )
                                      ]
                              )
                        ]

#3

@empet thanks! I’ll try to see if I can work something out.

On a side note, how do you post code that keeps its format?


#4

@wtfzambo For code formatting follow this link https://help.github.com/en/articles/creating-and-highlighting-code-blocks.