Astrum Astrum - 1 year ago 137
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)

are functions that return a tuple of the form

This plot gives me something close to what I want, but the vector lengths are all wrong. the two functions,
return a tuple of the point on a particular curve, and the
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

Answer Source

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')


Result Original:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download