import matplotlib.pyplot as plt
rows = 5
plt.figure(figsize=(6, 6))
for i in range(rows):
for j in range(i + 1):
plt.scatter(j, -i, s=500, c='green')
plt.xlim(-0.5, rows - 0.5)
plt.ylim(-rows + 0.5, 0.5)
plt.axis('off')
plt.gca().set_aspect('equal', adjustable='datalim')
plt.title("Right-Angled Triangle Pattern Plot", fontsize=14)
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Matplotlib
import matplotlib.pyplot as plt
This imports the pyplot module from Matplotlib, which is used for plotting graphs.
2. Defining the Number of Rows
rows = 5
The variable rows is set to 5, meaning the triangle will have 5 rows.
3. Creating the Figure
plt.figure(figsize=(6, 6))
This creates a 6x6 inch figure to ensure the plot is properly sized.
4. Generating the Right-Angled Triangle Pattern Using Nested Loops
for i in range(rows): # Loops through each row
for j in range(i + 1): # Number of dots increases in each row
plt.scatter(j, -i, s=500, c='green') # Green dots at (j, -i)
The outer loop (i) iterates over the number of rows.
The inner loop (j) controls the number of dots per row:
Row 0 → 1 dot
Row 1 → 2 dots
Row 2 → 3 dots
Row 3 → 4 dots
Row 4 → 5 dots
plt.scatter(j, -i, s=500, c='green') plots a green dot at (j, -i).
j represents the horizontal (x-axis) position.
-i represents the vertical (y-axis) position, using negative values to start from the top.
s=500 sets the dot size.
c='green' sets the dot color to green.
5. Setting the Axis Limits
plt.xlim(-0.5, rows - 0.5) # X-axis range
plt.ylim(-rows + 0.5, 0.5) # Y-axis range
These ensure that the dots fit well within the figure.
6. Removing the Axes & Formatting the Plot
plt.axis('off') # Removes the axis
plt.gca().set_aspect('equal', adjustable='datalim') # Ensures equal spacing
plt.title("Right-Angled Triangle Pattern Plot", fontsize=14) # Adds title
plt.axis('off') removes the x and y axes.
plt.gca().set_aspect('equal', adjustable='datalim') ensures that the spacing between dots remains uniform.
plt.title("Right-Angled Triangle Pattern Plot", fontsize=14) adds a title with font size 14.
7. Displaying the Plot
plt.show()
Renders and displays the final pattern.
0 Comments:
Post a Comment