import matplotlib.pyplot as plt
rows,cols=4,6
plt.figure(figsize=(6,4))
for i in range(rows):
for j in range(cols):
plt.scatter(j,-i,s=500,c='blue')
plt.xlim(-0.5,cols-0.5)
plt.ylim(-rows+0.5,0.5)
plt.axis('off')
plt.gca().set_aspect('equal',adjustable='datalim')
plt.title('Reactangular pattern plot',fontsize=14)
plt.show()
#source code --> clcoding.com
Code Explanation:
Importing Matplotlib:
import matplotlib.pyplot as plt
This imports Matplotlib's pyplot module, which is used for plotting.
Defining the grid size:
rows, cols = 4, 6
The pattern consists of 4 rows and 6 columns, forming a 4x6 rectangular grid.
Creating a figure:
plt.figure(figsize=(6, 4))
This creates a figure with a 6x4 inch size.
Generating the pattern using nested loops:
for i in range(rows):
for j in range(cols):
plt.scatter(j, -i, s=500, c='blue')
The outer loop (i) iterates over the rows.
The inner loop (j) iterates over the columns.
plt.scatter(j, -i, s=500, c='blue') places a blue dot at each (j, -i) coordinate:
j represents the x-coordinate (column index).
-i represents the y-coordinate (negative row index, keeping the origin at the top-left).
s=500 sets the dot size.
c='blue' sets the color to blue.
Setting the plot limits:
plt.xlim(-0.5, cols - 0.5)
plt.ylim(-rows + 0.5, 0.5)
plt.xlim(-0.5, cols - 0.5) ensures that the x-axis starts slightly before 0 and extends to cols - 1.
plt.ylim(-rows + 0.5, 0.5) adjusts the y-axis to properly contain all points.
Hiding the axis and adjusting the aspect ratio:
plt.axis('off')
plt.gca().set_aspect('equal', adjustable='datalim')
plt.axis('off') removes the x and y axes for a cleaner look.
plt.gca().set_aspect('equal', adjustable='datalim') ensures that the spacing between points remains uniform.
Adding a title:
plt.title("Rectangle Pattern Plot", fontsize=14)
Sets the title of the plot to "Rectangle Pattern Plot" with a font size of 14.
Displaying the plot:
plt.show()
Renders and displays the pattern.
0 Comments:
Post a Comment