Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';我的程序无法在matplotlib中以二维方式设置圆的动画_Python_Animation_Matplotlib_Plot - Fatal编程技术网

Python Can';我的程序无法在matplotlib中以二维方式设置圆的动画

Python Can';我的程序无法在matplotlib中以二维方式设置圆的动画,python,animation,matplotlib,plot,Python,Animation,Matplotlib,Plot,我试图用matplotlib在2D中设置n圆圈的动画,但运行时,我的代码仅显示一个静止的圆圈,无论我如何制作n。有人知道如何修复它吗?它有什么问题吗 %matplotlib notebook import numpy as np from matplotlib import pyplot as plt from matplotlib import animation fig=plt.figure() ax = plt.axes(xlim=(0, 40), ylim=(0, 40)) numb

我试图用
matplotlib
在2D中设置
n
圆圈的动画,但运行时,我的代码仅显示一个静止的圆圈,无论我如何制作
n
。有人知道如何修复它吗?它有什么问题吗

%matplotlib notebook
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig=plt.figure()
ax = plt.axes(xlim=(0, 40), ylim=(0, 40))

number_of_particles = int(input("How many particles would you like?"))
data = np.zeros((1000,number_of_particles))
dict_of_circles = {}
for n in range(number_of_particles):
    data[:,n] = [20*(1+np.sin(float(x)/50*n+50)) for x in range(1000)]
    dict_of_circles["circle"+str(n+1)] = plt.Circle((data[0,n],1.0),0.5,fc='b')


def init():
    for n in range(number_of_particles):
        dict_of_circles["circle"+str(n+1)].center = (data[0,n],1)
        ax.add_patch(dict_of_circles["circle"+str(n+1)])
        return dict_of_circles["circle"+str(n+1)]

def animate(i):
    for n in range(number_of_particles):
        dict_of_circles["circle"+str(n+1)].center = (data[i,n],1)
        return dict_of_circles["circle"+str(n+1)]

anim=animation.FuncAnimation(fig,animate,init_func=init,frames=1000,blit=True)

plt.show()
两个问题:

  • 压痕是错误的。您需要从函数返回,而不是从循环中返回
  • 您需要返回一个艺术家列表,而不是一个艺术家列表,因为您希望所有艺术家都更新
  • 完整代码:

    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib import animation
    
    fig=plt.figure()
    ax = plt.axes(xlim=(0, 40), ylim=(0, 40))
    
    number_of_particles = 10
    data = np.zeros((1000,number_of_particles))
    dict_of_circles = {}
    for n in range(number_of_particles):
        data[:,n] = [20*(1+np.sin(float(x)/50*n+50)) for x in range(1000)]
        dict_of_circles["circle"+str(n+1)] = plt.Circle((data[0,n],1.0),0.5,fc='b')
    
    
    def init():
        for n in range(number_of_particles):
            dict_of_circles["circle"+str(n+1)].center = (data[0,n],1)
            ax.add_patch(dict_of_circles["circle"+str(n+1)])
        return dict_of_circles.values()
    
    def animate(i):
        for n in range(number_of_particles):
            dict_of_circles["circle"+str(n+1)].center = (data[i,n],1)
        return dict_of_circles.values()
    
    anim=animation.FuncAnimation(fig,animate,init_func=init,frames=1000,blit=True)
    
    plt.show()