Friday, 14 August 2026

How to Create the Indian Flag in Python | Ashoka Chakra with 24 Spokes

 


How to Draw the Indian National Flag in Python Using NumPy and Matplotlib ๐Ÿ‡ฎ๐Ÿ‡ณ

Python is not only useful for data science and automation—it can also be used to create meaningful graphical illustrations. In this tutorial, we will draw the Indian National Flag (Tiranga) using Python, NumPy, and Matplotlib.

The program creates the three-color flag and draws the Ashoka Chakra with 24 equally spaced spokes at the center.

๐Ÿ‡ฎ๐Ÿ‡ณ Indian National Flag Specifications

Before writing the code, it is important to understand the basic specifications of the Indian National Flag.

According to the Flag Code of India, 2002, the flag:

  • Has three equal horizontal panels.

  • Uses India saffron (Kesari) at the top.

  • Has white in the middle.

  • Uses India green at the bottom.

  • Contains a navy-blue Ashoka Chakra in the center of the white panel.

  • The Ashoka Chakra has 24 equally spaced spokes.

  • Has a rectangular 3:2 length-to-height ratio.

The Flag Code has also been amended to allow hand-spun/hand-woven or machine-made cotton, polyester, wool, silk, or khadi bunting for physical flags. Those material requirements are separate from creating a digital Python illustration.

๐Ÿ Libraries Used

We only need two main Python libraries:

import numpy as np
import matplotlib.pyplot as plt

We also use Rectangle and Circle from Matplotlib to construct the flag and Ashoka Chakra.

from matplotlib.patches import Rectangle, Circle

๐Ÿ“ Creating the Flag

We use a width of 3 and a height of 2 to maintain the required 3:2 ratio.

width = 3
height = 2
band = height / 3

Since the flag contains three equal panels, each band has a height of:

2 / 3

๐ŸŽจ Adding the Three Bands

The three colors are added using Matplotlib's Rectangle patch.

for i, color in enumerate([green, white, saffron]):
    ax.add_patch(
        Rectangle(
            (0, i * band),
            width,
            band,
            facecolor=color,
            edgecolor="none"
        )
    )

The list is written from bottom to top because Matplotlib's coordinate system starts at the bottom:

Green
White
Saffron

Visually, the result is:

Saffron
White
Green

๐Ÿ”ต Creating the Ashoka Chakra

The Chakra is positioned at the exact center of the flag:

cx = width / 2
cy = height / 2

We then create the outer Chakra circle:

chakra_radius = band * 0.45

ax.add_patch(
    Circle(
        (cx, cy),
        chakra_radius,
        fill=False,
        color=navy,
        linewidth=3
    )
)

๐Ÿ”น Adding 24 Spokes

The Ashoka Chakra contains 24 equally spaced spokes.

NumPy makes calculating the angles easy:

for i in range(24):
    angle = 2 * np.pi * i / 24

For every angle, we calculate the starting and ending points of the spoke:

x1 = cx + inner_radius * np.cos(angle)
y1 = cy + inner_radius * np.sin(angle)

x2 = cx + chakra_radius * np.cos(angle)
y2 = cy + chakra_radius * np.sin(angle)

Then Matplotlib draws the spoke:

ax.plot(
    [x1, x2],
    [y1, y2],
    color=navy,
    linewidth=1.5
)

๐Ÿ’ป Complete Python Code

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Circle

width = 3
height = 2
band = height / 3

saffron = "#FF671F"
white = "#FFFFFF"
green = "#046A38"
navy = "#06038D"

fig, ax = plt.subplots(figsize=(12, 8))

for i, color in enumerate([green, white, saffron]):
    ax.add_patch(
        Rectangle(
            (0, i * band),
            width,
            band,
            facecolor=color,
            edgecolor="none"
        )
    )

cx = width / 2
cy = height / 2

chakra_radius = band * 0.45

ax.add_patch(
    Circle(
        (cx, cy),
        chakra_radius,
        fill=False,
        color=navy,
        linewidth=3
    )
)

inner_radius = chakra_radius * 0.12

ax.add_patch(
    Circle(
        (cx, cy),
        inner_radius,
        fill=False,
        color=navy,
        linewidth=2
    )
)

for i in range(24):
    angle = 2 * np.pi * i / 24

    x1 = cx + inner_radius * np.cos(angle)
    y1 = cy + inner_radius * np.sin(angle)

    x2 = cx + chakra_radius * np.cos(angle)
    y2 = cy + chakra_radius * np.sin(angle)

    ax.plot(
        [x1, x2],
        [y1, y2],
        color=navy,
        linewidth=1.5
    )

ax.set_xlim(0, width)
ax.set_ylim(0, height)
ax.set_aspect("equal")
ax.axis("off")

plt.tight_layout()
plt.show()

๐Ÿ“š What You Learn From This Project

This small Python project demonstrates several useful concepts:

  • NumPy trigonometric functions

  • for loops

  • Matplotlib figures and axes

  • Rectangles and circles

  • Coordinate systems

  • Sine and cosine

  • Angles and radians

  • Mathematical visualization

  • Drawing geometric patterns with Python

The project is a great example of how mathematics + Python + visualization can be combined to create something meaningful.

๐Ÿ‡ฎ๐Ÿ‡ณ Final Result

The program generates a digital representation of the Indian National Flag with:

Saffron + White + Green + Navy Blue Ashoka Chakra + 24 Spokes

The official Ministry of Home Affairs continues to publish the Flag Code and related guidance, including the 2021 and 2022 amendments.

Note: This Python program is an educational digital illustration. Compliance requirements for an actual physical National Flag—including material, manufacture, display, and handling—are governed separately by the Flag Code of India and the Prevention of Insults to National Honour Act.

๐Ÿš€ Conclusion

Drawing the Indian National Flag with Python is a simple but powerful visualization project. It shows that Python can go beyond traditional programming tasks and can be used to create geometric artwork and educational visualizations.

If you are learning NumPy and Matplotlib, this is a great beginner-friendly project to understand how mathematical coordinates, loops, and graphical objects work together.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (335) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (332) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (88) Coursera (302) Cybersecurity (34) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (418) Data Strucures (18) Deep Learning (214) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (382) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1359) Python Coding Challenge (1217) Python Mathematics (10) Python Mistakes (51) Python Quiz (599) Python Tips (99) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (19) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)