Sunday, February 25, 2024

PMAE 3 : grid plot of regular polygons

Python Matplotlib Animation Example 3

grid plot of regular polygons

The Python code utilizes the Matplotlib library to generate an animated grid plot of regular polygons. Employing trigonometric calculations, the code positions the polygons in a 4x4 grid and animates the plot, revealing diverse polygons over time. Each subplot features a regular polygon with an increasing number of sides, and the animation updates continuously, creating a dynamic and visually appealing pattern. The code achieves this by defining a function getdata to compute the vertices of regular polygons and utilizes the animation module of Matplotlib to update the plot at regular intervals. In summary, the code leverages Matplotlib to create an animated grid plot, showcasing a captivating sequence of regular polygons evolving in a visually compelling manner.



The code creates an animated grid of regular polygons, where each polygon has a different number of sides (from 3 to 18). The polygons are positioned in a 4x4 grid and smoothly transition between different shapes as the animation progresses. It utilizes trigonometry for accurate polygon generation and animation techniques for dynamic visualization, resulting in a visually appealing grid of evolving polygon shapes.

Python source code

import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from math import cos, sin, tan, pi

fig = plt.figure(figsize=(3,3), facecolor="#FFFFFF")
ax = plt.axes([0, 0, 1, 1])
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.axis('off')

def getdata(n):
nth = int(500/n)*n+1
dth = 2*pi/(nth-1)
sth = 2*pi/n
xs, ys = [], []
for ith in range(nth):
theta = ith*dth
rth = int(theta/sth)*sth
x1 = cos(sth/2)
y1 = tan(sth/2-(theta-rth))*x1
x2 = x1*cos(rth) + y1*sin(rth)
y2 = -x1*sin(rth) + y1*cos(rth)
xs.append(x2)
ys.append(y2)
return xs, ys

nbin = 16
pl = [0 for i in range(nbin)]

def update(i):
if i > 0 and i%nbin == 0:
for j in range(nbin):
pl[j].set_data([], [])
ibin = i%nbin
ix = ibin%4
iy = ibin/4
n = ibin + 3
x0 = ix*1.0/4.0+1.0/8.0
y0 = (4-iy)*1.0/4.0-1.0/8.0
xs, ys = getdata(n)
for j in range(len(xs)):
xs[j] = xs[j]*0.1 + x0
ys[j] = ys[j]*0.1 + y0
pl[ibin] = ax.plot(xs, ys, color="#0000FF")[0]

def draw(i):
update(i)

ani = animation.FuncAnimation(fig, draw, interval=200)

plt.show()


This code creates an animation of a grid filled with regular polygons with varying numbers of sides.

■ Setting up the environment:
• Imports necessary libraries for plotting and animation (`matplotlib.pyplot`, `matplotlib.animation`, `math`).
   • Creates a figure and an axes object for plotting.
• Sets the axes limits and removes unnecessary labels.

■ Generating polygon data:
• Defines the `getdata` function:
• Takes the number of sides (`n`) as input.
• Calculates the number of points needed for a smooth curve (`nth`).
• Iterates through each point and calculates its coordinates using trigonometric functions.
• Returns lists of x and y coordinates for the polygon.

■ Animation loop:
• Defines `nbin` as the total number of plots in the grid (16 in this case).
• Creates a list `pl` to store plot objects.
• Defines the `update` function:
• Clears previous plots at regular intervals.
• Calculates the index (`ibin`) for the current plot in the animation sequence.
• Determines the number of sides for the current polygon based on `ibin`.
• Calculates the position of the polygon within the grid.
• Generates polygon coordinates using `getdata`.
• Updates the corresponding plot in the list with the new polygon.
• Defines the `draw` function:
• Calls `update` to update the plot for each frame.

■ Running the animation:
• Creates an animation object using `FuncAnimation`.
• Sets the figure, update function, and frame interval (200 milliseconds).
• Displays the animation using `plt.show()`.

Overall, this code effectively utilizes trigonometry and animation techniques to create a visually appealing visualization of regular polygons with varying side counts.

No comments:

Post a Comment