<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Animation
All (or most of) the changes in scatter and quiver plots are (linearly) interpolated. On top top that, scatter plots and quiver plots can take a sequence of arrays (the first dimension), where only one array is visualized. Together this can make smooth animations with coarse timesteps. Lets see an example.
Step1:  Basic animation
Step2:  You can control which array to visualize, using the scatter.sequence_index property. Actually, the pylab.animate_glyphs is connecting the Slider and Play button to that property, but you can also set it from Python.
Step3:  Animating color and size
We now demonstrate that you can also animate color and size
Step4:  Now we only animate the z component
Step5:  color is of the wrong shape, the last dimension should contain the rgb value, i.e. the shape of should be (15, 2500, 3)
Step6:  Creating movie files
We now make a movie, with a 2 second duration, where we rotate the camera, and change the size of the scatter points.
Step7:  Resulting gif file
Animated quiver
Not only scatter plots can be animated, quiver as well, so the direction vector (vx, vy, vz) can also be animated, as shown in the example below, which is a (subsample of) a simulation of a small galaxy orbiting a host galaxy (not visible).
<ASSISTANT_TASK:>
Python Code:
import ipyvolume as ipv
import numpy as np
Explanation: Animation
All (or most of) the changes in scatter and quiver plots are (linearly) interpolated. On top top that, scatter plots and quiver plots can take a sequence of arrays (the first dimension), where only one array is visualized. Together this can make smooth animations with coarse timesteps. Lets see an example.
End of explanation
# only x is a sequence of arrays
x = np.array([[-1, -0.8], [1, -0.1], [0., 0.5]])
y = np.array([0.0, 0.0])
z = np.array([0.0, 0.0])
ipv.figure()
s = ipv.scatter(x, y, z, marker='sphere', size=10)
ipv.xyzlim(-1, 1)
ipv.animation_control(s) # shows controls for animation controls
ipv.show()
Explanation: Basic animation
End of explanation
s.sequence_index = 1
Explanation: You can control which array to visualize, using the scatter.sequence_index property. Actually, the pylab.animate_glyphs is connecting the Slider and Play button to that property, but you can also set it from Python.
End of explanation
# create 2d grids: x, y, and r 
u = np.linspace(-10, 10, 25)
x, y = np.meshgrid(u, u)
r = np.sqrt(x**2+y**2)
print("x,y and z are of shape", x.shape)
# and turn them into 1d
x = x.flatten()
y = y.flatten()
r = r.flatten()
print("and flattened of shape", x.shape)
Explanation: Animating color and size
We now demonstrate that you can also animate color and size
End of explanation
# create a sequence of 15 time elements
time = np.linspace(0, np.pi*2, 15)
z = np.array([(np.cos(r + t) * np.exp(-r/5)) for t in time])
print("z is of shape", z.shape)
# draw the scatter plot, and add controls with animate_glyphs
ipv.figure()
s = ipv.scatter(x, z, y, marker="sphere")
ipv.animation_control(s, interval=200)
ipv.ylim(-3,3)
ipv.show()
# Now also include, color, which containts rgb values
color = np.array([[np.cos(r + t), 1-np.abs(z[i]), 0.1+z[i]*0] for i, t in enumerate(time)])
size = (z+1)
print("color is of shape", color.shape)
Explanation: Now we only animate the z component
End of explanation
color = np.transpose(color, (0, 2, 1)) # flip the last axes
ipv.figure()
s = ipv.scatter(x, z, y, color=color, size=size, marker="sphere")
ipv.animation_control(s, interval=200)
ipv.ylim(-3,3)
ipv.show()
Explanation: color is of the wrong shape, the last dimension should contain the rgb value, i.e. the shape of should be (15, 2500, 3)
End of explanation
# This is commented out, otherwise it would run on readthedocs
# def set_view(figure, framenr, fraction):
#     ipv.view(fraction*360, (fraction - 0.5) * 180, distance=2 + fraction*2)
#     s.size = size * (2+0.5*np.sin(fraction * 6 * np.pi))
# ipv.movie('wave.gif', set_view, fps=20, frames=40)
Explanation: Creating movie files
We now make a movie, with a 2 second duration, where we rotate the camera, and change the size of the scatter points.
End of explanation
import ipyvolume.datasets
stream = ipyvolume.datasets.animated_stream.fetch()
print("shape of steam data", stream.data.shape) # first dimension contains x, y, z, vx, vy, vz, then time, then particle
fig = ipv.figure()
# instead of doing x=stream.data[0], y=stream.data[1], ... vz=stream.data[5], use *stream.data
# limit to 50 timesteps to avoid having a huge notebook
q = ipv.quiver(*stream.data[:,0:50,:200], color="red", size=7)
ipv.style.use("dark") # looks better
ipv.animation_control(q, interval=200)
ipv.show()
# fig.animation = 0 # set to 0 for no interpolation
Explanation: Resulting gif file
Animated quiver
Not only scatter plots can be animated, quiver as well, so the direction vector (vx, vy, vz) can also be animated, as shown in the example below, which is a (subsample of) a simulation of a small galaxy orbiting a host galaxy (not visible).
End of explanation
<END_TASK>