简体   繁体   English

为什么我的图表没有绘制 linspace 生成的点? (动画)

[英]Why does my graph not plot the points generated by linspace? (animation)

When I remove linspace and plot points by typing them into a list by hand they are plotted just fine.当我通过手动将它们输入列表来删除 linspace 和绘图点时,它们被绘制得很好。 However switch to linspace, and the points on the graph come up blank.但是切换到 linspace,图表上的点变成空白。 What am I missing here?我在这里想念什么? Printing the linspace lists show they are generating the values, but they don't seem to make the graph打印 linspace 列表显示它们正在生成值,但它们似乎没有制作图表

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)

It seems like you are facing a problem because of Position = [P] and Time = [T] .由于Position = [P]Time = [T] ,您似乎遇到了问题。

Because numpy.linspace already returns an array, you don't need additional [] .因为numpy.linspace已经返回一个数组,所以您不需要额外的[]

Here is a working example that is referenced from matplotlib tutorial .这是一个引用自matplotlib 教程的工作示例。

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

def init():
    ax.set_xlim(0, 100)
    ax.set_ylim(0, 100)
    return ln,

def update(i):
    xdata.append(T[i])
    ydata.append(P[i])
    ln.set_data(xdata, ydata)
    return ln,

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

fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro')

ani = FuncAnimation(fig, update, frames=np.arange(len(T)),
                    init_func=init, blit=True)

plt.show()

It is not as easy as your written code and, also, not related to np.linspace , AIK.它不像您编写的代码那么容易,而且与np.linspace 、AIK 无关。 Combining Choi answer (or matplotlib example) with another related SO post we can do this job using some code like:Choi 答案(或 matplotlib 示例)与另一篇相关的 SO 帖子相结合,我们可以使用以下代码完成这项工作:

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

def init():
    ax1.set_xlim(0, 100)
    ax1.set_ylim(0, 100)
    ax2.set_xlim(0, 100)
    ax2.set_ylim(0, 100)
    return ln, ln2

def update(i, Position, Time, P2, T2, ln, ln2):
    x.append(Time[i])
    y.append(Position[i])
    x2.append(T2[i] + 10)
    y2.append(P2[i] + 10)
    ln.set_data(x, y)
    ln2.set_data(x2, y2)
    return ln, ln2

Position = P2 = np.linspace(1, 50, 100)
Time = T2 = np.linspace(1, 50, 100)

fig = plt.figure(figsize=(6, 4))
plt.title("Image That's Moving")
plt.xticks([])
plt.yticks([])
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
x, y, x2, y2 = [], [], [], []
ln, = ax1.plot([], [], 'bo')
ln2, = ax2.plot([], [], 'ro')

ani = FuncAnimation(fig, update, frames=Time.shape[0], fargs=(Position, Time, P2, T2, ln, ln2), init_func=init, blit=True)
ani.save('test.gif', writer='imagemagick', fps=30)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM