import matplotlib.pyplot as plt
size=10
fig,ax=plt.subplots(figsize=(6,6))
for i in range(size):
plt.plot([i,size-1-i],[i,i],'bo')
plt.plot([i,size-1-i],[size-1-i,size-1-i],'bo')
ax.set_xlim(-1,size)
ax.set_ylim(-1,size)
ax.set_xticks([])
ax.set_yticks([])
ax.set_frame_on(False)
plt.title('X shaped pattern')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Import Required Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy is imported but not used in the code. It is typically helpful for numerical computations.
matplotlib.pyplot is used to create the plot.
2. Define Grid Size
size = 10 # Change this for a larger or smaller X shape
This variable size determines how large the "X" shape will be.
It represents the number of rows and columns in the pattern.
3. Create Figure and Axis
fig, ax = plt.subplots(figsize=(6, 6))
plt.subplots() creates a figure (fig) and an axis (ax).
figsize=(6,6) sets the size of the figure to 6 inches by 6 inches.
4. Generate X-Shaped Pattern
for i in range(size):
plt.plot([i, size - 1 - i], [i, i], 'bo') # Top-left to bottom-right
plt.plot([i, size - 1 - i], [size - 1 - i, size - 1 - i], 'bo') # Bottom-left to top-right
This loop iterates from 0 to size-1.
The first plt.plot():
Plots points along the diagonal from top-left to bottom-right (\).
(i, i) and (size - 1 - i, i) define the start and end points of a line.
The second plt.plot():
Plots points along the diagonal from bottom-left to top-right (/).
(i, size - 1 - i) and (size - 1 - i, size - 1 - i) define the points.
5. Set Axis Limits
ax.set_xlim(-1, size)
ax.set_ylim(-1, size)
Ensures that the plotted points fit well within the graph area.
Limits are slightly beyond the grid (-1 to size) to provide padding.
6. Remove Axes for a Cleaner Look
ax.set_xticks([])
ax.set_yticks([])
ax.set_frame_on(False)
ax.set_xticks([]) and ax.set_yticks([]) remove the tick marks.
ax.set_frame_on(False) removes the surrounding box.
7. Display the Plot
plt.show()
This command displays the generated X-shaped pattern.
0 Comments:
Post a Comment