import matplotlib.pyplot as plt
rows = 5
plt.figure(figsize=(6, 6))
for i in range(rows):
for j in range(rows - i):
plt.scatter(j, -i, s=800, c='red')
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("Inverted Right-Angled Triangle Pattern Plot",fontsize=14)
plt.show()Code Explanation:
1. Importing Matplotlib
import matplotlib.pyplot as plt
This imports pyplot from Matplotlib, which is used for plotting.
2. Defining the Number of Rows
rows = 5
This variable sets the number of rows to 5, meaning the triangle will have 5 levels.
3. Creating the Figure
plt.figure(figsize=(6, 6))
Creates a 6x6 inch figure for proper visualization.
4. Generating the Inverted Triangle Pattern Using Nested Loops
for i in range(rows): # Loops through each row (i)
for j in range(rows - i): # Decreasing number of dots in each row
plt.scatter(j, -i, s=800, c='red') # Red dots
The outer loop (i) iterates over the number of rows.
The inner loop (j) ensures that the number of dots decreases in each row:
Row 0 → 5 dots
Row 1 → 4 dots
Row 2 → 3 dots
Row 3 → 2 dotsRow 4 → 1 dot
plt.scatter(j, -i, s=800, c='red') places a red dot at (j, -i):
j → Represents the horizontal (x-axis) position.
-i → Represents the vertical (y-axis) position (negative value makes it start from the top).
s=800 → Sets the dot size to 800 for better visibility.
c='red' → Sets the dot color to red.
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
Ensures the plot area is properly adjusted to fit the triangle.
6. Removing the Axes & Formatting the Plot
plt.axis('off') # Hides the axes
plt.gca().set_aspect('equal', adjustable='datalim') # Ensures equal spacing
plt.title("Inverted Right-Angled Triangle Pattern Plot", fontsize=14) # Adds title
plt.axis('off') → Removes the x and y axes for a cleaner look.
plt.gca().set_aspect('equal', adjustable='datalim') → Ensures that dot spacing remains uniform.
plt.title("Inverted Right-Angled Triangle Pattern Plot", fontsize=14) → Adds a title with font size 14.
7. Displaying the Final Output
plt.show()
Renders and displays the triangle pattern.
0 Comments:
Post a Comment