Happy Independence Day! Today is July 4th, and what better way to celebrate than by honoring the iconic Stars and Stripes with a beautiful animation using Python
? In this tutorial, you'll learn how to generate a faithful, waving American flag with accurate proportions using matplotlib
.
A Brief History of the American Flag
The American flag, officially known as the "Stars and Stripes," represents the 50 states with stars and the 13 original colonies with alternating red and white stripes. Its modern design was codified by Executive Order 10834 in 1959, under President Dwight D. Eisenhower. This order defines exact geometric proportions for all parts of the flag.
Official Dimensions from Executive Order 10834
Component | Proportion | Variable |
---|---|---|
Flag Width | 1.9 units | A |
Flag Height | 1.0 unit | B |
Union Width | 0.76 × A | D |
Union Height | 7/13 × B | C |
Stripe Height | 1/13 × B | E |
Star Diameter | 0.0616 | F |
Star Spacing | Horiz: 0.063, Vert: 0.054 | G, H |
Star Offsets | Left: 0.063, Top: 0.054 | K, L |
Step-by-Step Code Tutorial
1. Imports and Setup
import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.animation as animation
2. Define Flag Dimensions
A = 1.9 # width B = 1.0 # height C = 7/13 # union height D = 0.76 # union width E = 1/13 # stripe height F = 0.0616 # star diameter
3. Create the Flag Canvas
fig, ax = plt.subplots(figsize=(10, 5)) ax.set_xlim(0, A) ax.set_ylim(0, B) ax.set_aspect('equal') ax.axis('off')
4. Draw Waving Stripes
for i in range(13): color = 'red' if i % 2 == 0 else 'white' y_base = i * E y_wave = y_base + 0.015 * np.sin(2 * np.pi * x / A + wave_offset + i) ax.fill_between(x, y_wave, y_wave + E, color=color)
5. Draw the Blue Union (Canton)
x_union = np.linspace(0, D, 300) y_union = 0.015 * np.sin(2 * np.pi * x_union / D + wave_offset) ax.fill_between(x_union, B - C + y_union, B + y_union, color='navy')
6. Draw the 50 Stars
for row in range(9): for col in range(11): if (row + col) % 2 == 1: continue cx = K + col * G cy = B - (L + row * H) + 0.015 * np.sin(2 * np.pi * cx / D + wave_offset) draw_star(ax, (cx, cy), star_radius)
Full Working Code
Now that you understand each piece, here's the complete code:
import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.animation as animation # Proportions A = 1.9 B = 1.0 C = 7 / 13 D = 0.76 E = 1 / 13 F = 0.0616 G = 0.063 H = 0.054 K = 0.063 L = 0.054 star_rows = 9 star_cols = 11 star_radius = F / 2 fig, ax = plt.subplots(figsize=(10, 5)) ax.set_xlim(0, A) ax.set_ylim(0, B) ax.set_aspect('equal') ax.axis('off') def draw_star(ax, center, radius, color='white'): cx, cy = center points = [] for i in range(10): angle = i * np.pi / 5 - np.pi / 2 r = radius if i % 2 == 0 else radius * 0.382 x = cx + r * np.cos(angle) y = cy + r * np.sin(angle) points.append((x, y)) star = patches.Polygon(points, closed=True, color=color) ax.add_patch(star) def update(frame): ax.clear() ax.set_xlim(0, A) ax.set_ylim(0, B) ax.set_aspect('equal') ax.axis('off') wave_offset = frame / 10.0 x = np.linspace(0, A, 300) # Stripes for i in range(13): color = 'red' if i % 2 == 0 else 'white' y_base = i * E y_wave = y_base + 0.015 * np.sin(2 * np.pi * x / A + wave_offset + i) ax.fill_between(x, y_wave, y_wave + E, color=color) # Union x_union = np.linspace(0, D, 300) y_union = 0.015 * np.sin(2 * np.pi * x_union / D + wave_offset) ax.fill_between(x_union, B - C + y_union, B + y_union, color='navy') # Stars for row in range(star_rows): for col in range(star_cols): if (row + col) % 2 == 1: continue cx = K + col * G cy = B - (L + row * H) cy += 0.015 * np.sin(2 * np.pi * cx / D + wave_offset) if cx < D and cy > B - C: draw_star(ax, (cx, cy), star_radius) ani = animation.FuncAnimation(fig, update, frames=200, interval=50, blit=False) plt.show()
Happy Fourth of July!
With just Python and matplotlib
, you’ve created a faithful, animated American flag. This is a wonderful way to celebrate July 4th using code. Feel free to customize the wave amplitude, add a background, or export it as a GIF or MP4.
Happy Independence Day!
No comments:
Post a Comment