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