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
forloopsMatplotlib 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