gpt4 book ai didi

python - python 中的极圈动画

转载 作者:行者123 更新时间:2023-11-28 20:38:38 25 4
gpt4 key购买 nike

这是我的 python 文件,用于模拟一个圆的振荡动力学,它会永久扩展和收缩:

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

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

def init():
line.set_data([], [])
return line,

def animate(i):
theta = np.linspace(0, 2 * np.pi, 100)
r = np.sqrt(np.abs(np.sin(0.1 * i)))
x = r * np.cos(theta) + 1
y = r * np.sin(theta)
line.set_data(x, y)
return line,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)

plt.show()

但参数定义存在缺陷,因此结果不是动画圆,而是省略号。

我该如何解决这个问题?

最佳答案

尝试添加 plt.gca().set_aspect('equal', adjustable='box') 并修改您的 ylim 如下:

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

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-1, 1))
line, = ax.plot([], [], lw=2)

def init():
line.set_data([], [])
return line,

def animate(i):
theta = np.linspace(0, 2 * np.pi, 100)
r = np.sqrt(np.abs(np.sin(0.1 * i)))
x = r * np.cos(theta) + 1
y = r * np.sin(theta)
line.set_data(x, y)
return line,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)

plt.gca().set_aspect('equal', adjustable='box')
plt.show()

这将显示如下:

equal axis screenshot

关于python - python 中的极圈动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40506062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com