Python:动画 Matplotlib 错误“缺少 1 个必需的位置参数:'curr'
Python: Animation Matplotlib error 'missing 1 required positional argument: 'curr'
我正在尝试为我使用 GridSpec 设置的 4 个子图制作动画,我想在其中表示我用随机数设置的 4 个分布,我设置子图的代码,分布和函数动画如下:
import matplotlib
import matplotlib.animation as animation
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Create the figure and set the grid:
fig = plt.figure()
gspec = matplotlib.gridspec.GridSpec(6,6)
top_left = plt.subplot(gspec[0:2,0:3])
top_right = plt.subplot(gspec[0:2,3:], sharey = top_left)
bottom_left = plt.subplot(gspec[3:5,0:3], sharey = top_left)
bottom_right = plt.subplot(gspec[3:5,3:], sharey = top_left)
#Create the random variables:
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)
#Create the function:
n = 10000-100
def anim(curr, subplot = None, dist = None):
if curr >= 100:
if curr == n+100:
a.event_source.stop()
subplot.cla()
bins = np.arange(p1,p2,step)
subplot.hist(dist[:curr], bins = bins)
subplot.gca().set_ylabel('Frequency')
subplot.gca().set_xlabel('Value')
subplot.annotate('n={}'.format(curr+100))
#Create the necessary lists for looping:
subplots = [top_left, top_right, bottom_left, bottom_right]
bins = [np.arange(-6,2,0.5),np.arange(-1,15,0.5), np.arange(5,20,0.5), np.arange(13.5,20.5,0.5)]
dists = [x2,x2,x3,x4]
然后,我尝试使用以下 animation.FuncAnimation
函数循环:
for i in range(3):
a = animation.FuncAnimation(fig, anim(subplot = subplots[i], dist = dists[i]))
But I get the following error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call
last) in ()
2
3 for i in range(3):
----> 4 a = animation.FuncAnimation(fig, anim(subplot = subplots[i], dist = dists[i]))
TypeError: anim() missing 1 required positional argument: 'curr'
为什么要我指定 curr
?我认为 FuncAnimation
函数能够自己理解这是要循环的参数。我想我不太了解 animation
模块
的功能
注意:我不得不说我尝试了一个与此类似的代码,但只是针对一个单独的图,没有任何网格,也没有循环。在那种情况下,我不必在 anim 函数中指定 subplot
或 dist
参数,因为在定义 anim
函数时没有必要包含它们,因为我只有为了传递一个图,我只是在函数中指定了这些值,在那种情况下,我没有收到任何错误,但现在我必须指定更多的参数,我得到了它。
你必须像这样在 FUncAnimation 中调用不带参数的函数:
a = animation.FuncAnimation(fig, anim)
所以像这样更改您的代码以便能够做到这一点:
subplots = [top_left, top_right, bottom_left, bottom_right]
bins = [np.arange(-6,2,0.5),np.arange(-1,15,0.5), np.arange(5,20,0.5), np.arange(13.5,20.5,0.5)]
dists = [x2,x2,x3,x4]
n = 10000-100
def anim(curr):
for i in range(3):
if curr >= 100:
if curr == n+100:
a.event_source.stop()
subplots[i].cla()
subplots[i].hist(dists[i][:curr], bins = bins[i])
subplots[i].gca().set_ylabel('Frequency')
subplots[i].gca().set_xlabel('Value')
subplots[i].annotate('n={}'.format(curr+100))
a = animation.FuncAnimation(fig, anim)
您还必须更改 anim 函数,根据文档 anim 应该是 return 一系列 Artist 对象的函数。在您的情况下,动画不会 return 任何东西。所以你必须修复它
我正在尝试为我使用 GridSpec 设置的 4 个子图制作动画,我想在其中表示我用随机数设置的 4 个分布,我设置子图的代码,分布和函数动画如下:
import matplotlib
import matplotlib.animation as animation
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Create the figure and set the grid:
fig = plt.figure()
gspec = matplotlib.gridspec.GridSpec(6,6)
top_left = plt.subplot(gspec[0:2,0:3])
top_right = plt.subplot(gspec[0:2,3:], sharey = top_left)
bottom_left = plt.subplot(gspec[3:5,0:3], sharey = top_left)
bottom_right = plt.subplot(gspec[3:5,3:], sharey = top_left)
#Create the random variables:
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)
#Create the function:
n = 10000-100
def anim(curr, subplot = None, dist = None):
if curr >= 100:
if curr == n+100:
a.event_source.stop()
subplot.cla()
bins = np.arange(p1,p2,step)
subplot.hist(dist[:curr], bins = bins)
subplot.gca().set_ylabel('Frequency')
subplot.gca().set_xlabel('Value')
subplot.annotate('n={}'.format(curr+100))
#Create the necessary lists for looping:
subplots = [top_left, top_right, bottom_left, bottom_right]
bins = [np.arange(-6,2,0.5),np.arange(-1,15,0.5), np.arange(5,20,0.5), np.arange(13.5,20.5,0.5)]
dists = [x2,x2,x3,x4]
然后,我尝试使用以下 animation.FuncAnimation
函数循环:
for i in range(3):
a = animation.FuncAnimation(fig, anim(subplot = subplots[i], dist = dists[i]))
But I get the following error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 2 3 for i in range(3): ----> 4 a = animation.FuncAnimation(fig, anim(subplot = subplots[i], dist = dists[i]))
TypeError: anim() missing 1 required positional argument: 'curr'
为什么要我指定 curr
?我认为 FuncAnimation
函数能够自己理解这是要循环的参数。我想我不太了解 animation
模块
注意:我不得不说我尝试了一个与此类似的代码,但只是针对一个单独的图,没有任何网格,也没有循环。在那种情况下,我不必在 anim 函数中指定 subplot
或 dist
参数,因为在定义 anim
函数时没有必要包含它们,因为我只有为了传递一个图,我只是在函数中指定了这些值,在那种情况下,我没有收到任何错误,但现在我必须指定更多的参数,我得到了它。
你必须像这样在 FUncAnimation 中调用不带参数的函数:
a = animation.FuncAnimation(fig, anim)
所以像这样更改您的代码以便能够做到这一点:
subplots = [top_left, top_right, bottom_left, bottom_right]
bins = [np.arange(-6,2,0.5),np.arange(-1,15,0.5), np.arange(5,20,0.5), np.arange(13.5,20.5,0.5)]
dists = [x2,x2,x3,x4]
n = 10000-100
def anim(curr):
for i in range(3):
if curr >= 100:
if curr == n+100:
a.event_source.stop()
subplots[i].cla()
subplots[i].hist(dists[i][:curr], bins = bins[i])
subplots[i].gca().set_ylabel('Frequency')
subplots[i].gca().set_xlabel('Value')
subplots[i].annotate('n={}'.format(curr+100))
a = animation.FuncAnimation(fig, anim)
您还必须更改 anim 函数,根据文档 anim 应该是 return 一系列 Artist 对象的函数。在您的情况下,动画不会 return 任何东西。所以你必须修复它