Python 如何在不同的n值上制作动画?
我编写了一个代码,在xy平面上绘制一些点和线。它以给定的n值绘制所有内容。所以对于不同的n,我得到了我想要的图。但是我想为不同的n值,比如n=1,2,…,100,设置这些绘图的动画。但是我不能做这个动画 这里有人能帮我做这个吗?非常感谢。我将代码粘贴到此处: 我的代码Python 如何在不同的n值上制作动画?,python,animation,matplotlib,Python,Animation,Matplotlib,我编写了一个代码,在xy平面上绘制一些点和线。它以给定的n值绘制所有内容。所以对于不同的n,我得到了我想要的图。但是我想为不同的n值,比如n=1,2,…,100,设置这些绘图的动画。但是我不能做这个动画 这里有人能帮我做这个吗?非常感谢。我将代码粘贴到此处: 我的代码 import matplotlib as mpl mpl.rc('text', usetex = True) mpl.rc('font', family = 'serif') import matplotlib.pyplot a
import matplotlib as mpl
mpl.rc('text', usetex = True)
mpl.rc('font', family = 'serif')
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
fig = plt.subplots()
ax = plt.axes(xlim=(-1.2, 1.2), ylim=(-1.2, 1.2))
plt.gca().set_aspect('equal', adjustable='box')
plt.style.use(['ggplot','dark_background'])
plt.rcParams['figure.figsize'] = (12, 8)
n = 10 #I want to animate this n.
p = 2
for k in range(0,n,1):
theta1 = np.pi + 2*k*np.pi / n
theta2 = np.pi + 2*p*k*np.pi / n
x, y = np.cos(theta1), np.sin(theta1)
x1, y1 = np.cos(theta2), np.sin(theta2)
plt.scatter(x, y, s=50, c='violet', zorder=3)
plt.plot([x,x1], [y,y1], color = 'w')
circle = plt.Circle((0, 0), 1, color='c', fill=False, lw = 1)
ax.add_artist(circle)
#Customize the axes and gridlines:
ax.grid(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
#TickMarks Customization:
ax.set_xticks([])
ax.set_yticks([])
#plt.savefig('nthRoots.png', format='png', dpi=1000,bbox_inches='tight')
plt.show()
输出
是否可以在不同的值上设置n的动画
编辑:这里我没有唯一的散点图…所以我无法理解如何使用这些链接来完成这项工作
我的尝试
#Animation.
import matplotlib as mpl
mpl.rc('text', usetex = True) #for LaTex notation in the Plot
mpl.rc('font', family = 'serif')
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
from matplotlib import animation, rc
rc('animation', html='html5')
fig = plt.subplots()
ax = plt.axes(xlim=(-1.2, 1.2), ylim=(-1.2, 1.2))
plt.gca().set_aspect('equal', adjustable='box')
plt.style.use(['ggplot','dark_background'])
plt.rcParams['figure.figsize'] = (12, 8)
p = 2
#Plotting Function:
def f(n):
for k in range(0,n,1):
theta1 = np.pi + 2*k*np.pi / n
theta2 = np.pi + 2*p*k*np.pi / n
x, y = np.cos(theta1), np.sin(theta1)
x1, y1 = np.cos(theta2), np.sin(theta2)
plt.scatter(x, y, s=50, c='violet', zorder=3)
plt.plot([x,x1], [y,y1], color = 'w')
circle = plt.Circle((0, 0), 1, color='c', fill=False, lw = 1)
ax.add_artist(circle)
#Customize the axes and gridlines:
ax.grid(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
#TickMarks Customization:
ax.set_xticks([])
ax.set_yticks([])
plt.show()
#Now I try to define a function for animating n in f(n)
def animate(n):
f(n)
anim = animation.FuncAnimation(fig, animate,
frames=100, interval=100, blit=True)
#anim.save('Wave.mp4', writer = 'ffmpeg', fps = 2, dpi=500,extra_args=['-vcodec', 'libx264'])
这就是我所有的。。。但是这个想法不起作用……我想我必须正确定义动画(n)
。
任何建议。。。!谢谢。代码中有几个问题(大多数与动画无关)
- 在创建地物之前,需要定义rcParams
返回一个包含plt.subplot
和figure
轴的元组
- 使用光点时,动画必须返回艺术家对象序列。不过你可以把它关掉
应在脚本末尾调用一次plt.show()
import matplotlib as mpl
mpl.rc('font', family = 'serif')
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
from matplotlib import animation, rc
plt.rcParams['figure.figsize'] = (12, 8)
plt.style.use(['ggplot','dark_background'])
fig, ax = plt.subplots()
p = 2
#Plotting Function:
def f(n):
ax.clear()
ax.set(xlim=(-1.2, 1.2), ylim=(-1.2, 1.2))
ax.set_aspect('equal', adjustable='box')
for k in range(0,n,1):
theta1 = np.pi + 2*k*np.pi / n
theta2 = np.pi + 2*p*k*np.pi / n
x, y = np.cos(theta1), np.sin(theta1)
x1, y1 = np.cos(theta2), np.sin(theta2)
plt.scatter(x, y, s=50, c='violet', zorder=3)
plt.plot([x,x1], [y,y1], color = 'w')
circle = Circle((0, 0), 1, color='c', fill=False, lw = 1)
ax.add_artist(circle)
#Customize the axes and gridlines:
ax.grid(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
#TickMarks Customization:
ax.set_xticks([])
ax.set_yticks([])
anim = animation.FuncAnimation(fig, f, frames=100, interval=100, blit=False)
plt.show()
可能重复另一个链接@Bazingaa…谢谢,,,我了解一点你的第二个链接…但这都是散点图…在我的情况下,我还需要绘制线,圆圈等。我怎么做。。。?Sry如果它看起来很傻…我是动画新手…我也更喜欢
FuncAnimation
方法…我尝试使用您的链接来完成此操作…但它不适合我的工作…它工作正常…但它会为所有n值绘制f(n)并保留它们…是否可以在每次调用f(n)后清除屏幕…您可以清除每个步骤的轴。我更新了代码。