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