Wednesday 24 January 2024

Indian Flag using NumPy and Matplotlib in Python

 


Code : 

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def draw_tricolor_flag():
    # Create figure and axes
    fig, ax = plt.subplots()
    # Draw tricolor bands
    colors = ['#138808', '#ffffff', '#FF6103']
    for i, color in enumerate(colors):
        rect = patches.Rectangle((0, 2*i+1), width=9, height=2, facecolor=color, edgecolor='grey')
        ax.add_patch(rect)
    # Draw Ashoka Chakra circle
    chakra_radius = 0.8
    ax.plot(4.5, 4, marker='o', markerfacecolor='#000080', markersize=9.5)
    chakra = patches.Circle((4.5, 4), chakra_radius, color='#000080', fill=False, linewidth=7)
    ax.add_artist(chakra)

    # Draw 24 spokes in Ashoka Chakra
    for i in range(24):
        angle1 = np.pi * i / 12 - np.pi / 48
        angle2 = np.pi * i / 12 + np.pi / 48
        spoke = patches.Polygon([[4.5, 4],
                                 [4.5 + chakra_radius / 2 * np.cos(angle1),
                                  4 + chakra_radius / 2 * np.sin(angle1)],
                                 [4.5 + chakra_radius * np.cos(np.pi * i / 12),
                                  4 + chakra_radius * np.sin(np.pi * i / 12)],
                                 [4.5 + chakra_radius / 2 * np.cos(angle2),
                                  4 + chakra_radius / 2 * np.sin(angle2)]],
                                fill=True, closed=True, color='#000080')
        ax.add_patch(spoke)
    # Set equal axis and display the plot
    ax.axis('equal')
    plt.show()
# Call the function to draw the tricolor flag
draw_tricolor_flag()
#clcoding.com

Explanation:


Imports:

numpy for numerical operations.
matplotlib.pyplot for creating plots.
matplotlib.patches for creating shapes like rectangles and polygons.
Function Definition (draw_tricolor_flag):

Creates a figure and axes for the plot.
Drawing Tricolor Bands:

Three rectangles are drawn to represent the tricolor bands of the flag using the colors green ('#138808'), white ('#ffffff'), and saffron ('#FF6103').
Drawing Ashoka Chakra Circle:

A circle is drawn at the center of the plot representing the Ashoka Chakra. It is outlined with a blue color ('#000080').
Drawing 24 Spokes in Ashoka Chakra:

A loop calculates the coordinates for each spoke and uses patches.Polygon to draw each spoke. The spokes are drawn in blue ('#000080').
Setting Equal Axis and Displaying the Plot:

The axis is set to be equal, ensuring an equal aspect ratio, and the plot is displayed.
The comment #clcoding.com at the end of your code appears to be a website or identifier but doesn't affect the code's functionality.


Code Explanation 

Imports:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
numpy is imported as np for numerical operations.
matplotlib.pyplot is imported as plt for creating plots.
matplotlib.patches is imported as patches for creating shapes like rectangles and polygons.

Function Definition:
def draw_tricolor_flag():
This defines a function named draw_tricolor_flag.

Creating Figure and Axes:
fig, ax = plt.subplots()
This creates a figure and axes for the plot.

Drawing Tricolor Bands:
colors = ['#138808', '#ffffff', '#FF6103']
for i, color in enumerate(colors):
    rect = patches.Rectangle((0, 2*i+1), width=9, height=2, facecolor=color, edgecolor='grey')
    ax.add_patch(rect)
It defines three colors for the tricolor bands and iterates through them, drawing rectangles for each color.

Drawing Ashoka Chakra Circle:
chakra_radius = 0.8
ax.plot(4.5, 4, marker='o', markerfacecolor='#000080', markersize=9.5)
chakra = patches.Circle((4.5, 4), chakra_radius, color='#000080', fill=False, linewidth=7)
ax.add_artist(chakra)
It draws a circle at the center of the plot representing the Ashoka Chakra.

Drawing 24 Spokes in Ashoka Chakra:
for i in range(24):
    angle1 = np.pi * i / 12 - np.pi / 48
    angle2 = np.pi * i / 12 + np.pi / 48
    spoke = patches.Polygon([[4.5, 4], 
                             [4.5 + chakra_radius / 2 * np.cos(angle1), 
                              4 + chakra_radius / 2 * np.sin(angle1)], 
                             [4.5 + chakra_radius * np.cos(np.pi * i / 12), 
                              4 + chakra_radius * np.sin(np.pi * i / 12)], 
                             [4.5 + chakra_radius / 2 * np.cos(angle2), 
                              4 + chakra_radius / 2 * np.sin(angle2)]], 
                            fill=True, closed=True, color='#000080')
    ax.add_patch(spoke)
It uses a loop to draw 24 spokes in the Ashoka Chakra.

Setting Equal Axis and Displaying the Plot:
ax.axis('equal')
plt.show()
It ensures that the aspect ratio of the plot is equal and then displays the plot.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (117) C (77) C# (12) C++ (82) Course (62) Coursera (179) coursewra (1) Cybersecurity (22) data management (11) Data Science (95) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (748) Python Coding Challenge (221) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses