Python Matplotlib Animation Example 4
regular polygons resembling a Ferris wheel
This Python code utilizes the Matplotlib library to generate an animated Ferris wheel-like visualization featuring rotating regular polygons. The animation continues indefinitely, showcasing polygons with varying numbers of sides rotating around a central point. The update function dynamically adjusts the positions of the polygons based on a rotating angle, creating an engaging and visually intriguing effect. The rotation speed is controlled by the P parameter in the draw function, and each frame is updated at a 40-millisecond interval. Users can customize parameters such as the number of polygon sides, colors, and animation speed to achieve different visual effects. Overall, this code delivers a captivating and customizable representation of regular polygons in continuous rotation, resembling a Ferris wheel.
Python source code
This code creates an animation of regular polygons resembling a Ferris wheel. Here's a breakdown:
■ Imports:
• Libraries for plotting (`matplotlib.pyplot`) and animation (`matplotlib.animation`) are imported.
• Mathematical functions (cosine, sine, tangent, and pi) are imported from the `math` library.
■ Figure and Axes Setup:
• A figure is created with a size of 3x3 inches and a white background.
• Axes are added to the figure, and their limits and visibility are set for a clean appearance.
■ Data Generation Functions:
• `getdata(n)`: Generates a regular polygon with `n` sides. It calculates the x and y coordinates of each vertex based on the angle and radius.
• `getinit(n)`: Provides the initial position (x and y coordinates) for a polygon with `n` sides.
• `getposdata(n)`: Creates a smaller, shifted, and rotated version of the polygon based on the original data and initial position.
• `getwheel()`: Defines the large base polygon with smaller polygons attached at specific intervals, resembling the Ferris wheel structure.
■ Variable Initialization:
• Nine sets of data (x and y coordinates) are generated for each polygon using the functions above.
• Each set is assigned to a corresponding variable (`xs1`, `ys1`, etc.).
■ Plotting:
• Nine lines representing the polygons are plotted using `ax.plot` with different colors and line widths.
■ Rotation and Shifting Functions:
• `rotate(x, y, theta)`: Takes x and y coordinates and a rotation angle. It returns the rotated coordinates.
• `shift(x, y, n, theta)`: Takes x and y coordinates, the number of sides (`n`), and a rotation angle. It returns the shifted and rotated coordinates based on the initial position.
■ Animation Update Function:
• `update(theta)`: Takes a rotation angle (`theta`) as input.
• It updates the data (x and y coordinates) of each plotted line using the `shift` and `rotate` functions with appropriate arguments.
• This essentially rotates and shifts the individual polygons based on their defined parameters.
■ Animation Drawing Function:
• `draw(i)`: This function is called repeatedly during the animation loop.
• It calculates the rotation angle based on the current time and a desired animation period (`P`).
• It calls the `update` function to update the data for each polygon based on the calculated angle.
■ Animation Creation:
• `ani`: Creates an animation object using `FuncAnimation`.
• It specifies the figure (`fig`), the drawing function (`draw`), and the interval (40 milliseconds) between frame updates.
■ Display:
• `plt.show()`: Displays the generated animation.
Overall, this code effectively utilizes mathematical functions, data generation, animation techniques, and plotting libraries to create a visually appealing animation of regular polygons rotating like a Ferris wheel.