Ankit Ankit - 1 year ago 113
Python Question

3D animation using matplotlib that shows a body rotating about a fixed point

I have recently started using python. Currently I am working on a project that simulates the motion of binary starts about a center.

I want to represent the starts with a sphere rotating about a fixed center.
I have x, y, z values stored in three different lists.

I have looked a lot on internet but couldn't find any help. I successfully simulated it in 2D but I want a 3D version of it.

I am using matplotlib in Python.

Any help would be appreciable.

Alternate possibility: Can I get separate plots for each values of x, y and z so that I can stitch them and make an animation? My major problem is, how can I represent each (x, y, z) point with a sphere?

Answer Source

VPython would work great for this type of visualization. To get a 3D sphere, for example, you just use the command sphere(pos=(1,2,3)), and animation is super easy. VPython is basically designed for the type of visualization you are talking about, and it will run quickly, whereas in matplotlib you'd have have to start with the equations for a sphere, and it would run slowly.

Here's a simple animation that shows a ball bouncing on a plate (source):

from visual import *

floor = box (pos=(0,0,0), length=4, height=0.5, width=4,
ball = sphere (pos=(0,4,0), radius=1,
ball.velocity = vector(0,-1,0)
dt = 0.01

while 1:
    rate (100)
    ball.pos = ball.pos + ball.velocity*dt
    if ball.y < ball.radius:
        ball.velocity.y = abs(ball.velocity.y)
        ball.velocity.y = ball.velocity.y - 9.8*dt

ball, rendered in a visual python window

In the comments, others have recommended VTK (or MayaVi, which is a nice wrapper for VTK). VTK will work well, but is much harder to learn. I'll use VTK for complicated 3D visual data exploration, but for your problem, it's extreme overkill.