我在 matplotlib.animation 中遇到解包错误
I am getting an unpacking error in matplotlib.animation
我正在尝试为我的学校项目生成动画图。首先,我制作了 Collatz conjecture 的静态表示,但我的老师告诉我要为图形制作动画:
import math
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
a=int(input('Enter the desired number:'))
for n in range(a):
i=n
l=[n]
x=[]
def animation_function(i):
while i>1:
if i%2==0:
i=int(i/2)
else:
i=int(3*i+1)
l.append(i)
j=0
while j<len(l):
x.append(j+1)
j+=1
#plt.plot(x,l)
figure, ax=plt.subplots()
ax.set_xlim(0,len(x))
ax.set_ylim(0,max(l))
line, ax=plt.subplots()
line, animation=FuncAnimation(figure,
func = animation_function,
frames = np.arange(0, 10, 0.1),
interval = 10)
plt.show()
这是我遇到的错误
Warning (from warnings module):
File "C:\Users\SPARSH\AppData\Local\Programs\Python\Python310x+1.2.py", line 23
ax.set_xlim(0,len(x))
UserWarning: Attempting to set identical left == right == 0 results in singular transformations; automatically expanding.
Warning (from warnings module):
File "C:\Users\SPARSH\AppData\Local\Programs\Python\Python310x+1.2.py", line 24
ax.set_ylim(0,max(l))
UserWarning: Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.
Traceback (most recent call last):
File "C:\Users\SPARSH\AppData\Local\Programs\Python\Python310x+1.2.py", line 26, in <module>
line, animation=FuncAnimation(figure,
TypeError: cannot unpack non-iterable FuncAnimation object
我是 matplotlib 的新手,所以如果我遗漏了一些小而愚蠢的事情或者犯了一个大错误,请告诉我。
由于线条的长度不同,我会避免使用 FuncAnimation,而是使用 plt.ion()
的交互式绘图。我试图主要保留你的结构,但引入了一些变化。
首先,我去掉了 x-range 列表。这是您的列表的隐含信息l
,因此我们不必单独收集它。
其次,由于图表在绘制外部值时不会 auto-update,我们必须收集 x 和 y 轴的最大值,因此我们知道何时必须更新图表限制。
最后,我们绘制每条线对象一次,然后在每次新迭代中更新其值。实现可能如下所示:
import matplotlib.pyplot as plt
respon = "Y"
plt.ion()
while respon == "Y":
a=int(input('Enter the desired number:'))
fig, ax = plt.subplots()
xmax = 1
ymax = 1
for n in range(1, a+1):
i=n
l=[n]
line, = ax.plot([1, 1], [i, i], marker="o", label = str(i))
ax.legend()
while i>1:
if i%2==0:
i=int(i/2)
else:
i=int(3*i+1)
l.append(i)
line.set_ydata(l)
line.set_xdata(range(1, 1+len(l)))
ymax = max(ymax, i)
xmax = max(xmax, 1+len(l))
ax.set_xlim(0.5, xmax+0.5)
ax.set_ylim(0.5, ymax+0.5)
plt.pause(0.5)
respon = input("repeat? Y/n")
plt.close()
print("finished")
示例输出:
我正在尝试为我的学校项目生成动画图。首先,我制作了 Collatz conjecture 的静态表示,但我的老师告诉我要为图形制作动画:
import math
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
a=int(input('Enter the desired number:'))
for n in range(a):
i=n
l=[n]
x=[]
def animation_function(i):
while i>1:
if i%2==0:
i=int(i/2)
else:
i=int(3*i+1)
l.append(i)
j=0
while j<len(l):
x.append(j+1)
j+=1
#plt.plot(x,l)
figure, ax=plt.subplots()
ax.set_xlim(0,len(x))
ax.set_ylim(0,max(l))
line, ax=plt.subplots()
line, animation=FuncAnimation(figure,
func = animation_function,
frames = np.arange(0, 10, 0.1),
interval = 10)
plt.show()
这是我遇到的错误
Warning (from warnings module):
File "C:\Users\SPARSH\AppData\Local\Programs\Python\Python310x+1.2.py", line 23
ax.set_xlim(0,len(x))
UserWarning: Attempting to set identical left == right == 0 results in singular transformations; automatically expanding.
Warning (from warnings module):
File "C:\Users\SPARSH\AppData\Local\Programs\Python\Python310x+1.2.py", line 24
ax.set_ylim(0,max(l))
UserWarning: Attempting to set identical bottom == top == 0 results in singular transformations; automatically expanding.
Traceback (most recent call last):
File "C:\Users\SPARSH\AppData\Local\Programs\Python\Python310x+1.2.py", line 26, in <module>
line, animation=FuncAnimation(figure,
TypeError: cannot unpack non-iterable FuncAnimation object
我是 matplotlib 的新手,所以如果我遗漏了一些小而愚蠢的事情或者犯了一个大错误,请告诉我。
由于线条的长度不同,我会避免使用 FuncAnimation,而是使用 plt.ion()
的交互式绘图。我试图主要保留你的结构,但引入了一些变化。
首先,我去掉了 x-range 列表。这是您的列表的隐含信息l
,因此我们不必单独收集它。
其次,由于图表在绘制外部值时不会 auto-update,我们必须收集 x 和 y 轴的最大值,因此我们知道何时必须更新图表限制。
最后,我们绘制每条线对象一次,然后在每次新迭代中更新其值。实现可能如下所示:
import matplotlib.pyplot as plt
respon = "Y"
plt.ion()
while respon == "Y":
a=int(input('Enter the desired number:'))
fig, ax = plt.subplots()
xmax = 1
ymax = 1
for n in range(1, a+1):
i=n
l=[n]
line, = ax.plot([1, 1], [i, i], marker="o", label = str(i))
ax.legend()
while i>1:
if i%2==0:
i=int(i/2)
else:
i=int(3*i+1)
l.append(i)
line.set_ydata(l)
line.set_xdata(range(1, 1+len(l)))
ymax = max(ymax, i)
xmax = max(xmax, 1+len(l))
ax.set_xlim(0.5, xmax+0.5)
ax.set_ylim(0.5, ymax+0.5)
plt.pause(0.5)
respon = input("repeat? Y/n")
plt.close()
print("finished")
示例输出: