1

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

6
  • It's impossible for us to help you unless you tell us what pos2 and vec are. Also, please tell us what your expected output is. Lastly, is there a typo in your snippet? Is it supposed to be X = pos1(t)? pos1 is currently not referenced anywhere in your snippet. Commented Sep 20, 2016 at 2:58
  • I've edited the post to include the entire thing. The output I'd like to get is a plot of all the distance vectors, vec(t), from pos1 to pos2, at the position it was at at t. Commented Sep 20, 2016 at 3:07
  • The picture is what you want to get, not what your code does for you, right? Because you sample produces different output. Commented Sep 20, 2016 at 5:25
  • 1
    @DJV I really want to do an unanimated version of this: lh5.googleusercontent.com/… Commented Sep 20, 2016 at 5:32
  • I think the relative speeds of the two points in that gif you've linked is important. I'm guessing those are planets? Earth and Venus? Do you know how many revolutions one planet undergoes relative to the other in that figure? Commented Sep 20, 2016 at 14:04

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

How exactly does the line "data = np.stack((x0, x1, y0, y1)).T.reshape(-1, 2)" work? You're combining the arrays with the first arguement, but what about the T.reshape(-1,2)?
reshape(-1, 2) "groups" by 2 elements in a "row" (-1 allows not to calculate number of "rows", it says "calculate this dimension for me, based on others"). If we wont make T operation, reshape will iterate through whole x0 array, then through whole x1, ... , resulting array would be wrong: [[x0[0], x0[1]], [x0[2], x0[3]], ...]. With T operation we iterate correctly and get resulting array as: [[x0[0], x1[0]], [y0[0], y1[0]], [x0[1], x1[1]], [y0[1], y1[1]]]. This is correct data we want to feed to plot.
Changed it to use column_stack. It is probably clearer.
Ok, so the only thing I still am not clear on is why this is plotting the vectors. I guess I don't really understand how the plot function is working. What exactly is the * doing in front of data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.