Creating the Indian National Flag Using Python

Jaspreet
4 min readSep 16, 2023

--

In the realm of coding, there’s a unique and heartwarming way to express one’s patriotism — by creating the Indian National Flag using Python! In this tutorial, we’ll embark on a simple yet creative journey to generate the tricolor flag and the Ashoka Chakra using Python’s NumPy and Matplotlib libraries. Let’s celebrate the spirit of India and learn how to represent its iconic flag in code.

Our code begins by importing the necessary libraries, NumPy for numerical operations and Matplotlib for visualization. Then, we use matplotlib.patches to create three rectangular patches representing the saffron, white, and green bands of the flag.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
# Creating the tricolor bands
saffron_band = patches.Rectangle((0, 1), width=12, height=2, facecolor='green', edgecolor='grey')
white_band = patches.Rectangle((0, 3), width=12, height=2, facecolor='white', edgecolor='grey')
green_band = patches.Rectangle((0, 5), width=12, height=2, facecolor='#FF9933', edgecolor='grey')

In the code above, we begin by importing the necessary libraries: NumPy for numerical operations, and Matplotlib for creating visualizations. We then use matplotlib.patches to create three rectangular patches representing the saffron, white, and green bands of the Indian flag. Each Rectangle object is defined with specific coordinates, width, height, face color (fill color), and edge color (border color).

We then add these patches to our plot and proceed to create the Ashoka Chakra, the iconic symbol at the heart of the Indian flag.

The Ashoka Chakra: Symbol of Unity

The Ashoka Chakra is represented by a blue circle with 24 spokes. Using Matplotlib, we draw the circle and spokes as follows:

# Drawing the Ashoka Chakra
radius = 0.8
plt.plot(6, 4, marker='o', markerfacecolor='#000088ff', markersize=9.5)
chakra = plt.Circle((6, 4), radius, color='#000088ff', fill=False, linewidth=7)
plt.gca().add_artist(chakra)

we first define the radius of the Ashoka Chakra. We use plt.plot to create a blue circle at the center of the flag with a marker (marker style 'o') that represents the circle. The markerfacecolor parameter sets the color of the marker, and markersize determines its size.

Next, we draw the Ashoka Chakra itself as a Circle object. We specify its center coordinates, radius, color, whether it should be filled (fill=False), and its border width (linewidth=7). The add_artist method adds the Chakra to the plot.

# Adding the 24 spokes to the Ashoka Chakra
for i in range(0, 24):
p = 6 + radius/2 * np.cos(np.pi * i/12 + np.pi/48)
q = 6 + radius/2 * np.cos(np.pi * i/12 - np.pi/48)
r = 4 + radius/2 * np.sin(np.pi * i/12 + np.pi/48)
s = 4 + radius/2 * np.sin(np.pi * i/12 - np.pi/48)
t = 6 + radius * np.cos(np.pi * i/12)
u = 4 + radius * np.sin(np.pi * i/12)
plt.gca().add_patch(patches.Polygon([[6, 4], [p, r], [t, u], [q, s]], fill=True, closed=True, color='#000088ff'))

In this part, we use a loop to create 24 spokes around the Ashoka Chakra. The spokes are generated by calculating the coordinates of their endpoints based on trigonometric calculations. We use np.cos and np.sin functions to determine the x and y coordinates of each endpoint.

We then use patches.Polygon to create each spoke as a polygon with four vertices. These vertices are defined using the calculated coordinates. The fill=True parameter fills the spoke with color and closed=True ensures that the polygon is closed, forming a solid spoke. The color of the spokes is set to blue ('#000088ff').

Bringing It All Together

To complete our masterpiece, we add the tricolor bands and the Ashoka Chakra to the plot. By setting the axis to ‘off,’ we ensure a clean, flag-focused visualization. Finally, we display our creation using plt.show().

# Adding tricolor bands to the plot
plt.gca().add_patch(saffron_band)
plt.gca().add_patch(white_band)
plt.gca().add_patch(green_band)
# Display the Indian National Flag
plt.axis('off')
plt.show()

Conclusion

With this Python code, we’ve paid tribute to our nation by coding the Indian National Flag and the Ashoka Chakra. This simple yet powerful demonstration showcases the harmony of technology and patriotism. So, whether you’re a coder or just someone looking for a creative way to express your love for your country, this Python journey is a delightful one to embark upon.

To make it easy for those who want to try out the code themselves, here’s the entire code snippet:

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

# Creating the tricolor bands
saffron_band = patches.Rectangle((0, 1), width=12, height=2, facecolor='green', edgecolor='grey')
white_band = patches.Rectangle((0, 3), width=12, height=2, facecolor='white', edgecolor='grey')
green_band = patches.Rectangle((0, 5), width=12, height=2, facecolor='#FF9933', edgecolor='grey')

# Drawing the Ashoka Chakra
radius = 0.8
plt.plot(6, 4, marker='o', markerfacecolor='#000088ff', markersize=9.5)
chakra = plt.Circle((6, 4), radius, color='#000088ff', fill=False, linewidth=7)
plt.gca().add_artist(chakra)

# Adding the 24 spokes to the Ashoka Chakra
for i in range(0, 24):
p = 6 + radius/2 * np.cos(np.pi * i/12 + np.pi/48)
q = 6 + radius/2 * np.cos(np.pi * i/12 - np.pi/48)
r = 4 + radius/2 * np.sin(np.pi * i/12 + np.pi/48)
s = 4 + radius/2 * np.sin(np.pi * i/12 - np.pi/48)
t = 6 + radius * np.cos(np.pi * i/12)
u = 4 + radius * np.sin(np.pi * i/12)
plt.gca().add_patch(patches.Polygon([[6, 4], [p, r], [t, u], [q, s]], fill=True, closed=True, color='#000088ff'))

# Adding tricolor bands to the plot
plt.gca().add_patch(saffron_band)
plt.gca().add_patch(white_band)
plt.gca().add_patch(green_band)

# Display the Indian National Flag
plt.axis('off')
plt.show()

Let’s continue to explore the endless possibilities of coding while cherishing the rich heritage and symbolism of our beloved India! 🇮🇳🐍🖼️

--

--