# == Import Required Libraries =================================================
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# ATTENTION: Uncomment this line, if you're running on a Jupyter Notebook
# %matplotlib notebook
def next_frame(total_frame_count: int = 10):
"""
Generate random 2D frames of varying dimensions.
This function serves as a generator that yields 2D numpy arrays with random
values. The dimensions of these arrays range between 5 and 50, inclusive.
The generator will continue yielding arrays until the total_frame_count
reaches zero.
Parameters
----------
total_frame_count : int, optional
The number of frames to be generated. The default value is 10.
Yields
------
array_like
2D numpy array with random values. The dimensions of the array range
between 5 and 50, inclusive. In other words, the number points
each frame of the animation will have varies between 5 and 50.
Examples
--------
Use this function in a for-loop to generate and process frames:
>>> frame_generator = next_frame(3)
>>> for frame in frame_generator:
>>> print(frame.shape)
(30, 2)
(12, 2)
(48, 2)
Notes
-----
This function can be used to generate frames for an animation iteratively.
"""
while total_frame_count > 0:
yield np.random.rand(np.random.randint(5, 50), 2)
total_frame_count -= 1
def update(frame):
"""
Update a scatter plot with new data.
This function clears the current scatter plot in the 'ax' Axes object,
sets the plot limits, and then creates a new scatter plot based on the
provided 2D frame. The 'ax' Axes object must be pre-defined.
Parameters
----------
frame : array_like
A 2D array where each row represents a point in the scatter plot.
The first column represents the x-values, and the second column
represents the y-values.
Returns
-------
scat : PathCollection
A collection of paths that make up the scatter plot.
Raises
------
NameError
If 'ax' isn't defined in the scope where this function is called.
Examples
--------
This function can be used in animation generation:
>>> import matplotlib.pyplot as plt
>>> import matplotlib.animation as animation
>>> fig, ax = plt.subplots()
>>> ani = animation.FuncAnimation(fig, update, frames=next_frame(3))
>>> plt.show()
"""
# Clear the last scatter plot
ax.clear()
# Set the limits of your plot again
# NOTE: You might want to dynamically set these limits based on the new frame
# values that you're plotting.
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# Plot the new scatter plot
scat = ax.scatter(frame[:, 0], frame[:, 1])
return scat
# == Create the Animation ======================================================
# Create a figure and an Axes object
fig, ax = plt.subplots()
# Set the initial limits of your plot
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# Create the animation object
ani = FuncAnimation(fig, update, frames=next_frame, blit=True)
# Convert the animation to HTML5 video
video = ani.to_html5_video()
# Display the video
HTML(video)
1条答案
按热度按时间wlwcrazw1#
如果您的目标是创建一个动画图,该图在动画的每个帧上包含可变数量的点,则以下代码可能可以实现此目的:
字符串
请注意,也可以在每个帧上修改轴限制。