import matplotlib.pyplot as plt
x_horiz=[-1,1]
y_horiz=[0,0]
x_vert=[0,0]
y_vert=[-1,1]
plt.plot(x_horiz,y_horiz,'b-',linewidth=3)
plt.plot(x_vert,y_vert,'b-',linewidth=3)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.grid(True)
plt.title('Plus pattern plot')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Matplotlib
import matplotlib.pyplot as plt
This imports matplotlib.pyplot, which is used for plotting in Python.
2. Defining Coordinates for the Plus (+) Shape
x_horiz = [-1, 1]
y_horiz = [0, 0]
This represents the horizontal line of the plus sign.
The line extends from x = -1 to x = 1, while keeping y = 0.
x_vert = [0, 0]
y_vert = [-1, 1]
This represents the vertical line of the plus sign.
The line extends from y = -1 to y = 1, while keeping x = 0.
3. Plotting the Horizontal and Vertical Lines
plt.plot(x_horiz, y_horiz, 'b-', linewidth=3)
plt.plot(x_vert, y_vert, 'b-', linewidth=3)
plt.plot(x_horiz, y_horiz, 'b-', linewidth=3)
Plots the horizontal line in blue (b).
Uses a solid line (-).
linewidth=3 makes the line thicker.
plt.plot(x_vert, y_vert, 'b-', linewidth=3)
Plots the vertical line using the same style.
Together, these lines form a plus (+) sign.
4. Setting Axis Limits
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.xlim(-2, 2) sets the x-axis limits from -2 to 2.
plt.ylim(-2, 2) sets the y-axis limits from -2 to 2.
This ensures the plus sign is centered and visible.
5. Enabling Grid
plt.grid(True)
Adds a grid to the plot for better visualization.
6. Adding a Title
plt.title("Plus Pattern Plot")
Adds a title to the plot.
7. Displaying the Plot
plt.show()
This function renders and displays the plot.
0 Comments:
Post a Comment