动画两个圆 Python
Animating two circles Python
我有一个文本文件,上面有两个圆的坐标。我想制作两个粒子的实时动画。由于某种原因,以下代码崩溃
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import pandas as pd
df = pd.read_csv('/path/to/text/file.txt', sep=" ", header=None)
fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)
ax = plt.axes(xlim=(0, 60), ylim=(0, 60))
patch = plt.Circle((5, -5), 6, fc='y')
patch2 = plt.Circle((5, -5), 6, fc='y')
def init():
patch.center = (df[0][0], df[1][0])
patch2.center = (df[2][0], df[3][0])
ax.add_patch(patch)
return patch, patch2,
def animate(i):
x, y = patch.center
x2, y2 = patch2.center
x = df[0][i]
y = df[1][i]
x2 = df[2][i]
y2 = df[3][i]
patch.center = (x, y)
patch2.center = (x2, y2)
i += 1
return patch, patch2,
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=360,
interval=20,
blit=True)
plt.show()
从中获取数据的文本文件具有 X1、Y1、X2 和 Y2 列,其中 X1 表示第一个粒子的 x 坐标等
如果我删除对第二个粒子的所有引用,它会起作用并完全显示第一个。任何帮助将不胜感激。
文本文件的示例如下:
40.028255 30.003469 29.999967 20.042583 29.999279 29.997491
40.073644 30.001855 30.000070 20.090549 30.002644 29.997258
40.135379 29.996389 29.995906 20.145826 30.005277 29.997931
40.210547 29.998941 29.996237 20.197438 30.004859 30.002082
40.293916 30.002021 29.992079 20.248248 29.998585 30.006919
(此代码中未使用后面的列)并且确切的错误消息是“AttributeError: 'NoneType' object has no attribute '_get_view”。
谢谢
代码崩溃是因为您以错误的方式“混合”了 matplotlib 的“pyplot”和“object-oriented”方法。这是工作代码。请注意,我创建了 ax
,艺术家将被添加到其中的轴。在这个轴上我也应用了轴限制。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.patches import Circle
N = 360
df = pd.DataFrame({
"x1": np.linspace(0, 60, N),
"y1": np.linspace(0, 60, N),
"x2": np.flip(np.linspace(0, 60, N)),
"y2": np.linspace(0, 60, N),
})
fig = plt.figure()
ax = fig.add_subplot()
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
patch = Circle((5, -5), 6, fc='y')
patch2 = Circle((5, -5), 6, fc='y')
ax.add_artist(patch)
ax.add_artist(patch2)
def init():
patch.center = (df["x1"][0], df["y1"][0])
patch2.center = (df["x2"][0], df["y2"][0])
return patch, patch2
def animate(i):
x, y = patch.center
x2, y2 = patch2.center
x = df["x1"][i]
y = df["y1"][i]
x2 = df["x2"][i]
y2 = df["y2"][i]
patch.center = (x, y)
patch2.center = (x2, y2)
i += 1
return patch, patch2,
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=N,
interval=20,
blit=True)
plt.show()
我有一个文本文件,上面有两个圆的坐标。我想制作两个粒子的实时动画。由于某种原因,以下代码崩溃
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import pandas as pd
df = pd.read_csv('/path/to/text/file.txt', sep=" ", header=None)
fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)
ax = plt.axes(xlim=(0, 60), ylim=(0, 60))
patch = plt.Circle((5, -5), 6, fc='y')
patch2 = plt.Circle((5, -5), 6, fc='y')
def init():
patch.center = (df[0][0], df[1][0])
patch2.center = (df[2][0], df[3][0])
ax.add_patch(patch)
return patch, patch2,
def animate(i):
x, y = patch.center
x2, y2 = patch2.center
x = df[0][i]
y = df[1][i]
x2 = df[2][i]
y2 = df[3][i]
patch.center = (x, y)
patch2.center = (x2, y2)
i += 1
return patch, patch2,
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=360,
interval=20,
blit=True)
plt.show()
从中获取数据的文本文件具有 X1、Y1、X2 和 Y2 列,其中 X1 表示第一个粒子的 x 坐标等
如果我删除对第二个粒子的所有引用,它会起作用并完全显示第一个。任何帮助将不胜感激。
文本文件的示例如下:
40.028255 30.003469 29.999967 20.042583 29.999279 29.997491
40.073644 30.001855 30.000070 20.090549 30.002644 29.997258
40.135379 29.996389 29.995906 20.145826 30.005277 29.997931
40.210547 29.998941 29.996237 20.197438 30.004859 30.002082
40.293916 30.002021 29.992079 20.248248 29.998585 30.006919
(此代码中未使用后面的列)并且确切的错误消息是“AttributeError: 'NoneType' object has no attribute '_get_view”。
谢谢
代码崩溃是因为您以错误的方式“混合”了 matplotlib 的“pyplot”和“object-oriented”方法。这是工作代码。请注意,我创建了 ax
,艺术家将被添加到其中的轴。在这个轴上我也应用了轴限制。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.patches import Circle
N = 360
df = pd.DataFrame({
"x1": np.linspace(0, 60, N),
"y1": np.linspace(0, 60, N),
"x2": np.flip(np.linspace(0, 60, N)),
"y2": np.linspace(0, 60, N),
})
fig = plt.figure()
ax = fig.add_subplot()
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
patch = Circle((5, -5), 6, fc='y')
patch2 = Circle((5, -5), 6, fc='y')
ax.add_artist(patch)
ax.add_artist(patch2)
def init():
patch.center = (df["x1"][0], df["y1"][0])
patch2.center = (df["x2"][0], df["y2"][0])
return patch, patch2
def animate(i):
x, y = patch.center
x2, y2 = patch2.center
x = df["x1"][i]
y = df["y1"][i]
x2 = df["x2"][i]
y2 = df["y2"][i]
patch.center = (x, y)
patch2.center = (x2, y2)
i += 1
return patch, patch2,
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=N,
interval=20,
blit=True)
plt.show()