import numpy as np
import matplotlib.pyplot as plt
x,y=np.meshgrid(np.arange(0,10,1),np.arange(0,10,1))
u=np.cos(x)
v=np.sin(y)
plt.figure(figsize=(6,6))
plt.quiver(x,y,u,v,angles='xy',scale_units='xy',scale=2,color='b')
plt.xlim(-1,10)
plt.ylim(-1,10)
plt.title("Arrow pattern plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.show()
#source code --> clcoding.com
Code Explanation:
Import Necessary Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy is used to generate numerical data efficiently.
matplotlib.pyplot is used for plotting.
Create a Grid of Points
x, y = np.meshgrid(np.arange(0, 10, 1), np.arange(0, 10, 1))
np.meshgrid() generates a grid of (x, y) coordinates in the range 0 to 9.
np.arange(0, 10, 1) creates a sequence from 0 to 9 (step size 1).
This results in a 10×10 grid.
Define Arrow Directions
u = np.cos(x)
v = np.sin(y)
u = np.cos(x): The X-component of the arrow is calculated using cos(x), meaning arrow direction in the x-axis varies based on x.
v = np.sin(y): The Y-component of the arrow is calculated using sin(y), meaning arrow direction in the y-axis varies based on y.
Create a Figure & Plot the Arrows
plt.figure(figsize=(6, 6))
plt.quiver(x, y, u, v, angles='xy', scale_units='xy', scale=2, color='b')
plt.figure(figsize=(6,6)): Creates a 6×6-inch figure.
plt.quiver(x, y, u, v, ...):
x, y: Positions of arrows on the grid.
u, v: Arrow directions (vectors).
angles='xy': Ensures arrows follow a Cartesian coordinate system.
scale_units='xy': Ensures correct arrow scaling.
scale=2: Adjusts arrow size.
color='b': Sets the arrow color to blue.
Set Axis Limits & Labels
plt.xlim(-1, 10)
plt.ylim(-1, 10)
Defines the x-axis and y-axis limits from -1 to 10 for better visualization.
plt.title("Arrow Pattern plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
Sets title and axis labels to describe the plot.
plt.grid(True)
Enables grid lines for better readability.
Display the Plot
plt.show()
Displays the generated arrow pattern plot.
0 Comments:
Post a Comment