Home > other >  Why does my graph not plot the points generated by linspace? (animation)
Why does my graph not plot the points generated by linspace? (animation)

Time:06-14

When I remove linspace and plot points by typing them into a list by hand they are plotted just fine. However switch to linspace, and the points on the graph come up blank. What am I missing here? Printing the linspace lists show they are generating the values, but they don't seem to make the graph

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
%matplotlib qt

fig = plt.figure(figsize=(6,4))
axes = fig.add_subplot(1,1,1)
plt.title("Image That's Moving")

P=np.linspace(1,50,100)
T=np.linspace(1,50,100)

Position =[P]
Time = [T]
p2=[P]
t2=[T]


x,y=[],[]
x2,y2=[],[]


def animate(i):
    x.append(Time[i])
    y.append((Position[i]))
    x2.append(t2[i])
    y2.append((p2[i]))

    plt.xlim(0,100)
    plt.ylim(0,100)
    plt.plot(x,y, color="blue")
    plt.plot(x2,y2, color="red")



anim = FuncAnimation(fig, animate, interval=300)

CodePudding user response:

It seems like you are facing a problem because of Position = [P] and Time = [T].

Because numpy.linspace already returns an array, you don't need additional [].

Here is a working example that is referenced from enter image description here

  • Related