Tuesday, 4 March 2025

Mosaic Tile pattern plot using python


import numpy as np

import matplotlib.pyplot as plt

rows,cols=8,8

tile_size=1

fig,ax=plt.subplots(figsize=(8,8))

ax.set_xlim(0,cols*tile_size)

ax.set_ylim(0,rows*tile_size)

ax.set_aspect('equal')

ax.axis('off')

colors=['#6FA3EF','#F4C542','#E96A64','#9C5E7F']

for row in range(rows):

    for col in range(cols):

        x=col*tile_size

        y=row*tile_size

        color=np.random.choice(colors)

        square=plt.Rectangle((x,y),tile_size,tile_size,color=color,ec='white',lw=2)

        ax.add_patch(square)

plt.title('Mosaic tile pattern plot',fontsize=16,fontweight='bold',color='navy',pad=15)

plt.show()

#source code --> clcoding.com         

Code Explanation:

 Importing Necessary Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy (np) → Used for selecting random colors (np.random.choice)

matplotlib.pyplot (plt) → Used for creating and displaying the plot


Setting Grid Size and Tile Size

rows, cols = 10, 10  

tile_size = 1  

Defines a 10×10 grid (rows = 10, cols = 10)

Each tile (square) has a side length of 1 unit


Creating the Figure & Axes

fig, ax = plt.subplots(figsize=(8, 8))

Creates a figure (fig) and an axis (ax)

figsize=(8, 8) → Creates an 8×8-inch plot


Setting Axis Limits

ax.set_xlim(0, cols * tile_size)

ax.set_ylim(0, rows * tile_size)

ax.set_xlim(0, cols * tile_size) → X-axis ranges from 0 to cols * tile_size = 10 * 1 = 10

ax.set_ylim(0, rows * tile_size) → Y-axis ranges from 0 to rows * tile_size = 10 * 1 = 10

This ensures the entire grid fits within the defined space


Maintaining Square Tiles and Hiding Axes

ax.set_aspect('equal')

ax.axis('off')

ax.set_aspect('equal') → Ensures the tiles are perfect squares

ax.axis('off') → Hides the axes (grid lines, ticks, labels) for a clean look


Defining Colors for the Tiles

colors = ['#6FA3EF', '#F4C542', '#E96A64', '#9C5E7F']

A list of 4 hex color codes

Colors used:

#6FA3EF (Light Blue)

#F4C542 (Yellow)

#E96A64 (Red-Orange)

#9C5E7F (Dark Purple)

The colors are chosen randomly for each tile


Creating the Mosaic Tiles Using Nested Loops

for row in range(rows):

    for col in range(cols):

Outer loop (row) → Iterates through each row

Inner loop (col) → Iterates through each column

This ensures we fill all (10 × 10) = 100 tiles in the mosaic grid


Setting Tile Position and Assigning Random Color

x = col * tile_size  

y = row * tile_size  

color = np.random.choice(colors)  

x = col * tile_size → Calculates X-coordinate of the tile

y = row * tile_size → Calculates Y-coordinate of the tile

np.random.choice(colors) → Randomly selects a color for the tile


Drawing the Tiles

square = plt.Rectangle((x, y), tile_size, tile_size, color=color, ec='white', lw=2)

ax.add_patch(square)

plt.Rectangle((x, y), tile_size, tile_size, color=color, ec='white', lw=2)


Creates a square tile at (x, y)

Size = tile_size × tile_size (1×1)

color=color → Assigns the random color

ec='white' → Adds a white border around each tile

lw=2 → Sets the border width to 2

ax.add_patch(square) → Adds the tile to the plot


Adding a Title

plt.title("Mosaic Tile Pattern", fontsize=16, fontweight='bold', color='navy', pad=15)

Adds a title "Mosaic Tile Pattern" to the plot

Title settings:

fontsize=16 → Large text

fontweight='bold' → Bold text

color='navy' → Dark blue text

pad=15 → Adds space above the title


Displaying the Pattern

plt.show()

Displays the final mosaic tile pattern


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (248) 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 (36) 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 (1014) Python Coding Challenge (452) Python Quiz (94) 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

Python Coding for Kids ( Free Demo for Everyone)