Astrum Astrum - 2 months ago 4
Python Question

Plotting an array of vectors in Python (pyplot)

I'm trying to plot a large array of vectors using pyplot. Right now I've got

import matplotlib.pyplot as plt
import numpy as np
import operator

t = np.arange(0, np.pi, .01)


def pos1(t):
x1 = .72 * np.cos(13*t)
y1 = .72 * np.sin(13*t)
return x1, y1


def pos2(t):
x2 = np.cos(8*t)
y2 = np.sin(8*t)
return x2, y2


def vec(t):
x1 = pos1(t)[0]
x2 = pos2(t)[0]
y1 = pos1(t)[1]
y2 = pos2(t)[1]
x = np.subtract(x1, x2)
y = np.subtract(y1, y2)
return x, y


X = pos2(t)[0]
Y = pos2(t)[1]
U = vec(t)[0]
V = vec(t)[1]

plot1 = plt.figure()
plt.quiver(X, Y, U, V, headlength=4)
plt.show(plot1)


Where
pos1(t)
,
pos2(t)
and
vec(t)
are functions that return a tuple of the form
([a,...,z],[a1,...,z1])
.

This plot gives me something close to what I want, but the vector lengths are all wrong. the two functions,
pos1(t),pos2(t)
return a tuple of the point on a particular curve, and the
vec(t)
function is their difference, leading to a vector from a point on the first curve to a point on the second. My plot has the correct direction, but not magnitude.

enter image description here

DJV DJV
Answer

quiver handles length of arrows. It seems quiver is not what you need.

Using regular plot:

import numpy as np
import matplotlib.pyplot as plt


t = np.arange(0, 2 * np.pi, 0.01)
x0 = np.sin(8 * t)
y0 = np.cos(8 * t)
x1 = 0.72 * np.sin(13 * t)
y1 = 0.72 * np.cos(13 * t)

data = np.column_stack((x0, x1, y0, y1)).reshape(-1, 2)

plt.plot(*data, color='black')
plt.gca().set_aspect('equal', adjustable='box')
plt.show()

Result:

Result Original:

Original