import numpy as np
import matplotlib.pyplot as plt
rows,cols=10,10
radius=1
fig,ax=plt.subplots(figsize=(8,8))
ax.set_xlim(0,cols*radius)
ax.set_ylim(0,(rows+0.5)*(radius/2))
ax.set_aspect('equal')
ax.axis('off')
color=['#6FA3EF','#3D7A9E','#F4C542','#E96A64','#9C5E75']
for row in range(rows):
for col in range(cols):
x=col*radius
y=row*(radius/2)
if row%2==1:
x+=radius/2
semicircle=plt.cCircle((x,y),radius,color=np.random.choice(colors),clip_on=False)
ax.add_patch(semicircle)
plt.title('Fish scale pattern plot',fontsize=16,fontweight='bold',color='navy',pad=15)
plt.show()
#source code --> clcoding.com
Code Explanation:
Import required libraries
import numpy as np
import matplotlib.pyplot as plt
Imports numpy (as np) and matplotlib.pyplot (as plt)
numpy is used for mathematical operations and randomness
matplotlib.pyplot is used for creating plots
Setting Grid Size & Circle Radius:
rows, cols = 10, 10
radius = 1
Defines the number of rows and columns (10×10 grid)
Defines the radius of each semicircle as 1
Creating the Figure & Axes:
fig, ax = plt.subplots(figsize=(8,8))
Creates a figure (fig) and an axis (ax) with an 8×8-inch canvas
plt.subplots() is used for making a figure with subplots
Setting Axis Limits:
ax.set_xlim(0, cols * radius)
ax.set_ylim(0, (rows + 0.5) * (radius / 2))
ax.set_xlim(0, cols * radius) → X-axis ranges from 0 to cols * radius
ax.set_ylim(0, (rows + 0.5) * (radius / 2)) → Y-axis height is adjusted for proper alignment of semicircles
(rows + 0.5) * (radius / 2) ensures the last row is properly visible
Making the Plot Circular and Removing Axes:
ax.set_aspect('equal')
ax.axis('off')
ax.set_aspect('equal') → Maintains equal scaling for X and Y axes, ensuring circles remain perfectly round
ax.axis('off') → Removes the axes, labels, and ticks for a clean look
Defining Colors:
colors = ['#6FA3EF', '#3D7A9E', '#F4C542', '#E96A64', '#9C5E7F']
Creates a list of five colors (in hex format)
These colors will be randomly assigned to the semicircles
Looping to Create Fish Scales:
for row in range(rows):
for col in range(cols):
Outer loop (row) iterates through each row
Inner loop (col) iterates through each column
Calculating X & Y Positions:
x = col * radius
y = row * (radius / 2)
x = col * radius → Sets the X position for each semicircle
y = row * (radius / 2) → Sets the Y position for stacking semicircles half overlapping
Offsetting Alternate Rows:
if row % 2 == 1:
x += radius / 2
If row is odd, shift X position by radius / 2
This staggered alignment mimics the overlapping scale effect
Drawing the Semicircles:
semicircle = plt.Circle((x, y), radius, color=np.random.choice(colors), clip_on=False)
ax.add_patch(semicircle)
plt.Circle((x, y), radius, color=np.random.choice(colors), clip_on=False)
Creates a circle at (x, y) with radius = 1
Fills it with a random color from the color list (np.random.choice(colors))
clip_on=False ensures the circles aren't clipped at the edges
ax.add_patch(semicircle) → Adds the semicircle to the plot
Adding a Title:
plt.title("Fish Scale Pattern", fontsize=16, fontweight='bold', color='navy', pad=15)
Adds a title: "Fish Scale Pattern"
Font settings:
fontsize=16 → Large text
fontweight='bold' → Bold font
color='navy' → Dark blue text
pad=15 → Adds extra spacing above the title
Displaying the Pattern:
plt.show()
Displays the final fish scale pattern plot
0 Comments:
Post a Comment