gpt4 book ai didi

python - 是否可以在 matplotlib 中的动画过程中改变速度

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:24 24 4
gpt4 key购买 nike

简而言之,问题是:是否有可能(使用 matplotlib.animation 工具或其他 Python 模块)在动画的某些帧上获得慢动作? p>

一些背景:

我有一个 matplotlib 动画图,其中我改变一个变量并显示其他两个变量的等值线图。我的想法是在接近函数最大值时放慢动画速度,以便我可以更清楚地定位它,而在远离它的地方加速,而没有太多兴趣。

目前,我最好的想法是将最接近最大值的帧加倍,但是有人可以有更好的想法吗?

谢谢大家!

<小时/>

代码片段:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
X = np.linspace(1,10, 100)
Y = np.linspace(1,10, 100)
R = np.linspace(-1, 1, 100)
ims = []

for r in R:
z = func(X, Y, r)
im = plt.imshow(z)
ims.append(im)
if check_r(r):
ims.append(im)

其中 func() 是一个返回依赖于 r(len(X), len(Y)) 数组的函数(例如 Z[i,j] = X[i]**r * Y[j]**(1-r) 或其他,而 check_r()测试 r 是否在需要最大化的值范围内。

最佳答案

我认为你的想法是最好的。我找到了另一种使用 matplotlib 动画的方法。这个想法是通过做出相同的点来使用帧作为慢延迟。

在此示例中,仅绘制了正弦曲线,但它将应用其他函数。

( most of code is took from here )

import numpy as np
import matplotlib.animation as animation
import matplotlib.pylab as plt
import pandas as pd

TWOPI = 2*np.pi
fig, ax = plt.subplots()

# making frames "delay"
frames = np.arange(0.0, TWOPI, 0.1)
frames = np.insert(frames, 17, [1.7]*5)
frames = np.insert(frames, 16, [1.6]*5)
frames = np.insert(frames, 15, [1.5]*5)

t = np.arange(0.0, TWOPI, 0.001)
s = np.sin(t)
l = plt.plot(t, s)

ax = plt.axis([0,TWOPI,-1,1])

redDot, = plt.plot([0], [np.sin(0)], 'ro')

def animate(i):
redDot.set_data(i, np.sin(i))
return redDot,

myAnimation = animation.FuncAnimation(fig, animate, frames=frames,
interval=100, blit=True, repeat=True)

关于python - 是否可以在 matplotlib 中的动画过程中改变速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59749767/

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