widgetti / ipyvolume

3d plotting for Python in the Jupyter notebook based on IPython widgets using WebGL
MIT License
1.94k stars 234 forks source link

Trailing line of scattered animation #183

Open jerpint opened 5 years ago

jerpint commented 5 years ago

Is it possible to have a trailing scatter animation? I have multiple points on a surface being animated (i.e. random walks) and they are following a trajectory, however I would like to visualize the entire trail of these points through time as it happens (where did they go, did they go where I would want, etc.).

maartenbreddels commented 5 years ago

I think you could 'abuse' the Mesh to do that. For each keyframe, you would repeat the coordinates of the last frame, and then the line indices would point from the current to the previous. In the next keyframe again, etc. Do you understand the idea, if not I could give a more concrete example tomorrow.

jerpint commented 5 years ago

I think I get the idea, but not sure what the most 'clean' and 'pythonic' way to do this would be, do you have an example on hand?

jerpint commented 5 years ago

I should probably mention that I am computing values beforehand; it looks something like this:


xrange = np.arange(-xlim, xlim, 1*step)
yrange = np.arange(-ylim, ylim, 1*step)
x = np.meshgrid(xrange, yrange)

# Compute function over the entire meshgrid and plot
Z = f(x)
X, Y = x
fig = ipv.figure()
ipv.plot_mesh(X, Y, Z, color="cyan")

# Precompute results to some optimization
xx, yy, zz = some_optimization(*args)

# Scatter results
s = ipv.scatter(xx, yy, zz, size=1.25, color=colors, marker="sphere")
ipv.animation_control(s)
ipv.show()
maartenbreddels commented 5 years ago

Sth like this:

import ipyvolume as ipv
import numpy as np

N = 4
x0 = np.linspace(0, 1, N)
x1 = x0 + 0.1
x2 = x0 + 0.2
x3 = x0 + 0.3
y0 = x0*2.0
y1 = x1**2.2
y2 = x2**2.5
y3 = x3**2.7

fig = ipv.figure()
x = np.zeros((3, N*2))
y = np.zeros((3, N*2))
z = x * 0 + 0.5
x[0] = np.concatenate([x0, x1])
x[1] = np.concatenate([x1, x2])
x[2] = np.concatenate([x2, x3])
y[0] = np.concatenate([y0, y1])
y[1] = np.concatenate([y1, y2])
y[2] = np.concatenate([y2, y3])
lines = [[0,N], [1, 1+N], [2, 2+N], [3, 3+N]]
# lines = [0,3, 1, 3, 2, 4]
p = ipv.plot_trisurf(x, y, z, lines=lines);
ipv.animation_control(p)
ipv.show()