Monday, 3 March 2025

Fish Scale pattern plot using python


 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

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (247) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (142) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (9) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (78) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1012) Python Coding Challenge (452) Python Quiz (91) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

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

Python Coding for Kids ( Free Demo for Everyone)