Friday, July 4, 2025

Animating the Indian National Flag in Python — A Tribute to the Tiranga ๐Ÿ‡ฎ๐Ÿ‡ณ

Jai Hind! As we approach August 15, India's Independence Day, let’s celebrate by paying homage to the Tiranga — the Indian National Flag — using code! In this tutorial, you’ll learn how to generate a beautiful waving tricolour flag using Python and matplotlib.

๐Ÿ‡ฎ๐Ÿ‡ณ About the Tiranga

The Indian National Flag, adopted on July 22, 1947, is a horizontal tricolour of saffron at the top, white in the middle, and green at the bottom, with the Ashoka Chakra — a 24-spoked navy-blue wheel — in the center.

The flag’s design and proportions are governed by the Bureau of Indian Standards (IS 1:1968). Let’s stick closely to these standards while animating.

๐Ÿ“ Official Specifications

  • Flag Ratio: 3:2 (length to height)
  • Each stripe: Equal height (i.e., 1/3rd of flag height)
  • Ashoka Chakra: 24 spokes, navy blue, radius = 3/4 of the white band’s height

๐Ÿ’ป Python Animation of the Flag

๐ŸŽจ Steps We Will Follow

  1. Draw three equal horizontal bands of saffron, white, and green
  2. Draw a navy-blue Ashoka Chakra centered on the white band
  3. Apply a horizontal sine wave effect to simulate waving
  4. Animate with matplotlib.animation

๐Ÿ“ฆ Requirements

pip install matplotlib numpy

๐Ÿง‘‍๐Ÿ’ป Full Working Code

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.animation as animation

# Flag dimensions (aspect ratio 3:2)
width = 3.0
height = 2.0
stripe_height = height / 3

fig, ax = plt.subplots(figsize=(9, 6))
ax.set_xlim(0, width)
ax.set_ylim(0, height)
ax.set_aspect('equal')
ax.axis('off')

def draw_chakra(ax, center_x, center_y, radius):
    chakra = plt.Circle((center_x, center_y), radius, color='navy', fill=False, linewidth=2)
    ax.add_patch(chakra)
    for i in range(24):
        angle = 2 * np.pi * i / 24
        x_end = center_x + radius * np.cos(angle)
        y_end = center_y + radius * np.sin(angle)
        ax.plot([center_x, x_end], [center_y, y_end], color='navy', linewidth=1)

def update(frame):
    ax.clear()
    ax.set_xlim(0, width)
    ax.set_ylim(0, height)
    ax.set_aspect('equal')
    ax.axis('off')

    wave_offset = frame / 10.0
    x = np.linspace(0, width, 300)

    stripe_colors = ['#FF9933', 'white', '#138808']

    for i, color in enumerate(stripe_colors):
        y_base = height - (i + 1) * stripe_height
        y_wave = y_base + 0.05 * np.sin(2 * np.pi * x / width + wave_offset + i)
        ax.fill_between(x, y_wave, y_wave + stripe_height, color=color, zorder=0)

    cx = width / 2
    cy = stripe_height + stripe_height / 2
    cy += 0.05 * np.sin(2 * np.pi * cx / width + wave_offset + 1)
    draw_chakra(ax, cx, cy, 0.3)

ani = animation.FuncAnimation(fig, update, frames=200, interval=50)
plt.show()

๐ŸŽ† A Digital Salute

With just a few lines of code, we’ve created a digital version of the Indian national flag that waves with pride and grace. Customize it further, export as a GIF, or even sync it with music for a true patriotic vibe.

Vande Mataram! Jai Hind! ๐Ÿ‡ฎ๐Ÿ‡ณ

No comments: