如何解决Matplotlib 动画:如何制作反向动画? 我当前的动画:当前代码:
我目前正在研究 matplotlib 动画。目前在情节中,我有 2 个圆圈,绿色和橙色一个。我想要做的是缩小绿色圆圈直到它变为零(笛卡尔图的基础)。我得到的是一个从 (0,0) 坐标出来的新蓝色圆圈。如果我想让绿色圆圈缩小到 (0,0),我必须怎么做,因为绿色圆圈的中心也是 (0,0)。
谢谢您的建议
注意:我使用的是 3.9.2 python 和所有包的最新更新
我当前的动画:
当前代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig,ax = plt.subplots(1)
line,= ax.plot([],[],lw=2)
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
# Move left y-axis and bottim x-axis to centre,passing through (0,0)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# Show ticks in the left and lower axes only
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# theta goes from 0 to 2pi
theta = np.linspace(0,2*np.pi,100)
# the radius of the circle
r = np.sqrt(1)
r2 = np.sqrt(4)
# compute x1 and x2
x1 = 1+r*np.cos(theta)
y1 = r*np.sin(theta)
x2 = r2*np.cos(theta)
y2 = r2*np.sin(theta)
def init():
line.set_data([],[])
return line,def animate(i):
x2 = np.sqrt(i)*np.cos(theta)
y2 = np.sqrt(i)*np.sin(theta)
line.set_data(x2,y2)
return line,# create the figure
ax.plot(x1,y1)
ax.plot(x2,y2)
ax.set_aspect(1)
plt.grid()
anim = animation.FuncAnimation(fig,animate,init_func=init,frames=200,interval=10)
plt.show()
f = r"D:/UNPAR/Semester 2/Pemrograman Komputer/Project/animation.gif"
writergif = animation.PillowWriter(fps=30)
anim.save(f,writer=writergif)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。