Sunday, 2 March 2025

Arrow pattern plot using python

 


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

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (247) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (142) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (9) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (78) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1012) Python Coding Challenge (452) Python Quiz (91) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses

Python Coding for Kids ( Free Demo for Everyone)