Matplotlib animation step
I am creating a Matplotlib animation of a step function. I am using the following code ...
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.step([], [])
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 2, 10)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
plt.show()
This vaguely resembles what I want (something like a gif below), but instead of the values being constant and scrolling over time, each step is dynamic and moves up and down. How would my code change to achieve this change?
source to share
step
explicitly displays steps between input data points. He can never build a partial "step".
You want animation with "partial steps" in between.
Instead of using, ax.step
use ax.plot
but make a staggered series by building y = y - y % step_size
.
In other words, something like:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 1000) # Using a series of 1000 points...
y = np.sin(x)
# Make *y* increment in steps of 0.3
y -= y % 0.3
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
Notice the partial "steps" at the beginning and end
By incorporating this into our animation example, we end up with something similar to:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [])
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
y -= y % 0.3
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
plt.show()
source to share