1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
"""
===========================
How to use the LineAnimator
===========================
This example shows off some ways in which you can use the
LineAnimator object to animate line plots.
"""
import matplotlib.pyplot as plt
import numpy as np
from mpl_animators import LineAnimator
###############################################################################
# Animate a 2D cube of random data as a line plot along an
# axis where the x-axis drifts with time.
# Define some random data
data_shape0 = (10, 20)
rng = np.random.default_rng()
data0 = rng.random(data_shape0)
###############################################################################
# Define the axis that will make up the line plot.
plot_axis0 = 1
slider_axis0 = 0
###############################################################################
# Let's customize the values along the x-axis. To do this, we must define the
# edges of the pixels/bins being plotted along the x-axis. This requires us to
# supply an array, say xdata, of length equal to data.shape[plot_axis_index]+1.
# In this example, the data has a shape of (10, 20) and let's say we are
# iterating through the 0th axis and plotting the 1st axis,
# i.e. plot_axis_index=1. Therefore we need to define an xdata array of length
# 21.
# This will give the same customized x-axis values for each frame of the
# animation. However, what if we want the x-axis values to change as we
# animate through the other dimensions of the cube? To do this we supply a
# (10, 21) xdata where each row (i.e. xdata[i, :]) gives the pixel/bin edges
# along the x-axis for the of the i-th frame of the animation. Note that this
# API extends in the same way to higher dimension. In our 2D case here though,
# we can define our non-constant x-axis values like so:
xdata = np.tile(np.linspace(0, 100, (data_shape0[plot_axis0] + 1)), (data_shape0[slider_axis0], 1))
###############################################################################
# Generate animation object with variable x-axis data.
ani = LineAnimator(data0, plot_axis_index=plot_axis0, axis_ranges=[None, xdata]).get_animation()
plt.show()
|