How to make an animation of a Lissajous curve

2020-05-01 09:25发布

问题:

I want to make an animation of the curve (where you incrementally draw it) with parametrization: x(t) = sin(3t) and y(y) = sin(4t) where t[0, 2pi].

Attempt:

%matplotlib notebook

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

# set the minimum potential
rm = math.pow(2, 1 / 6)
t = np.linspace(-10, 10, 1000, endpoint = False)
x = []
y = []

for i in len(t): #TypeError 'int' object is not iterable
    x_i = np.sin( 3 * t[i] )
    y_i = np.sin( 4 * t[i] )
    x.append(x_i)
    y.append(y_i)

# plot the graph
plt.plot(x,y)

# set the title
plt.title('Plot sin(4t) Vs sin(3t)')

# set the labels of the graph
plt.xlabel('sin(3t)')
plt.ylabel('sin(4t)')

# display the graph
plt.show()

But TypeError 'int' object is not iterable springs up... How can I fix it?

Thanks

回答1:

You're trying to iterate over an integer (i in len(t))

Instead, you need to be iterating over the array:

for i in t:
    x_i = np.sin( 3 * i )
    y_i = np.sin( 4 * i )
    x.append(x_i)
    y.append(y_i)