Friday, July 4, 2025

Animating the American Flag in Python — A July 4th Tribute

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

ComponentProportionVariable
Flag Width1.9 unitsA
Flag Height1.0 unitB
Union Width0.76 × AD
Union Height7/13 × BC
Stripe Height1/13 × BE
Star Diameter0.0616F
Star SpacingHoriz: 0.063, Vert: 0.054G, H
Star OffsetsLeft: 0.063, Top: 0.054K, 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: