Nic*_*ran 2 python animation matplotlib
我想知道是否有人可以向我解释 , 逗号在行中的作用
P, = ax.plot([],[])
Q, = ax.plot([],[])
Qa, =ax.plot([],[])
Run Code Online (Sandbox Code Playgroud)
当我使用没有这些逗号的代码时,我收到一个错误,向我解释列表没有 set_data 属性。我整个上午都在试图破译它,但到目前为止一直无法破译。完整的代码如下。它是运行简短动画的小程序的一部分。感谢你的帮助。
from math import cos, sin , pi
from matplotlib import pyplot as plt
import numpy as np
from matplotlib import animation as anim
theta = np.linspace(0,2*pi, 100)
#psi = pi/3
a = 2
b = 1
x = []
y = []
for angle in theta:
x.append(a*cos(angle))
y.append(b*sin(angle))
fig = plt.figure(figsize=(8,8), dpi=80)
plt.subplot(111)
ax = plt.axes(xlim =(-2.5, 2.5), ylim = ( -2.5, 2.5))
plt.plot(x,y)
P, = ax.plot([],[])
Q, = ax.plot([],[])
Qa, =ax.plot([],[])
#plt.plot([0,a*cos(psi)],[0,b*sin(psi)], label = "P")
#plt.plot([0,a*cos(psi + pi/2)],[0,b*sin(psi + pi/2)], label = "Q")
#plt.plot([0, a*cos(psi-pi/2)],[0,b*sin(psi- pi/2)], label = "Q'")
plt.legend()
def update(frame):
psi = pi*frame/ 100
P.set_data([0,a*cos(psi)],[0,b*sin(psi)])
Q.set_data([0,a*cos(psi + pi/2)],[0,b*sin(psi + pi/2)])
Qa.set_data([0, a*cos(psi-pi/2)],[0,b*sin(psi- pi/2)])
return P, Q, Qa
def init():
P.set_data([],[])
Q.set_data([],[])
Qa.set_data([],[])
return P, Q, Qa
animation = anim.FuncAnimation(fig, update, interval=10, blit= False, init_func = init, frames=200)
# animation.save('rain.gif', writer='imagemagick', fps=30, dpi=40)
plt.show()
Run Code Online (Sandbox Code Playgroud)
ax.plot()返回一个tuple只包含一个元素的。如果不带逗号分配它,则只需分配tuple.
>>> t = (1,)
>>> a = t
>>> a
(1,)
Run Code Online (Sandbox Code Playgroud)
但是如果你用逗号分配它,你就解压了它。
>>> t = (1,)
>>> a, = t
>>> a
1
Run Code Online (Sandbox Code Playgroud)