[How To] Draw Vectors in Python
import matplotlib.pyplot as plt
x_pos = np.linspace(0,5,10)
y_pos = np.linspace(0,10,10)
x_dir = y_dir = np.zeros((10,10))
plt.quiver(x_pos, y_pos, x_dir, y_dir, scale=1)
x_pos = [2.5,2.5]
y_pos = [5,5]
x_dir = [1.5,1]
y_dir = [1.5,2.5]
plt.quiver(x_pos, y_pos, x_dir, y_dir, scale=10)
x1, y1 = [0,5], [5,5]
x2, y2 = [2.5,2.5], [0,10]
plt.plot(x1, y1, x2, y2)
x_pos = [3.75,3.75]
y_pos = [9.75,9.75]
x_dir = [-1.5,-1]
y_dir = [-1.5,-2.5]
plt.quiver(x_pos, y_pos, x_dir, y_dir, scale=10)
plt.axis('off')
plt.show()
This example draws vectors, arrows with a position and direction, in Python using Matplotlib's quiver function, which takes the starting points and the x and y components of each arrow.
Vector field plots like this are useful in physics and math for showing flows, gradients, and forces. Tuning the scale keeps the arrows readable rather than overlapping.
Comments
Post a Comment