Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to make Indian Flag in Python
Python's libraries for drawing graphs have very extensive features which can not only give us charts but also provide flexibility to draw other diagrams like flags. In that sense, those modules have an artistic touch. In this article we will see how to draw the Indian flag using the libraries NumPy and Matplotlib.
Approach
Create three rectangles of same width and draw them with appropriate colors and borders representing the tricolor.
Use pyplot function to draw the circle of the Ashoka Chakra at the center of the middle rectangle.
Use NumPy and Matplotlib to draw the 24 spokes inside the Ashoka Chakra.
In all the above drawing we specify the colors, borders, radius and line lengths to match the proportions of the Indian flag.
Creating the Indian Flag
We implement the above approach using the following program which creates the three horizontal stripes and the Ashoka Chakra in the center ?
import numpy as np
import matplotlib.pyplot as py
import matplotlib.patches as patch
# Plotting the tri colors in national flag
# Green stripe (bottom)
green_stripe = patch.Rectangle((0,1), width=12, height=2, facecolor='green', edgecolor='grey')
# White stripe (middle)
white_stripe = patch.Rectangle((0,3), width=12, height=2, facecolor='white', edgecolor='grey')
# Saffron stripe (top)
saffron_stripe = patch.Rectangle((0,5), width=12, height=2, facecolor='#FF9933', edgecolor='grey')
# Create the plot
fig, ax = py.subplots()
ax.add_patch(green_stripe)
ax.add_patch(white_stripe)
ax.add_patch(saffron_stripe)
# Ashoka Chakra Circle
radius = 0.8
# Center point of the chakra
py.plot(6, 4, marker='o', markerfacecolor='#000088ff', markersize=9.5)
# Main circle of the chakra
chakra = py.Circle((6, 4), radius, color='#000088ff', fill=False, linewidth=7)
ax.add_artist(chakra)
# 24 spokes in 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)
ax.add_patch(patch.Polygon([[6,4], [p,r], [t,u], [q,s]], fill=True, closed=True, color='#000088ff'))
# Set equal aspect ratio and display
py.axis('equal')
py.title('Indian Flag')
py.show()
The output of the above code is ?
How It Works
The program creates the Indian flag by following these key steps:
Rectangle Creation: Three rectangles represent the saffron, white, and green stripes of the flag
Chakra Circle: A blue circle is drawn at the center of the white stripe
Spoke Generation: Using trigonometric functions, 24 spokes are drawn as polygons radiating from the center
Color Scheme: Official colors are used ? saffron (#FF9933), white, green, and navy blue (#000088ff)
Key Components
| Component | Method Used | Purpose |
|---|---|---|
| Flag Stripes | Rectangle patches | Creates the tricolor base |
| Chakra Circle | Circle artist | Outer ring of Ashoka Chakra |
| Spokes | Polygon patches | 24 radial lines inside chakra |
Conclusion
This program demonstrates how Python's Matplotlib and NumPy libraries can be used for creative graphical projects beyond simple charts. The combination of geometric shapes, trigonometry, and color management creates an accurate representation of the Indian flag.
