Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 步骤的Matplotlib动画_Python_Animation_Matplotlib_Trigonometry - Fatal编程技术网

Python 步骤的Matplotlib动画

Python 步骤的Matplotlib动画,python,animation,matplotlib,trigonometry,Python,Animation,Matplotlib,Trigonometry,我正在创建步骤函数的Matplotlib动画。我正在使用以下代码 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.step([], []) def init(): line.set_data([], [])

我正在创建步骤函数的Matplotlib动画。我正在使用以下代码

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.step([], [])

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

def animate(i):
    x = np.linspace(0, 2, 10)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

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

plt.show()
它隐约像我想要的(类似下面的gif),但不是值是恒定的,随着时间滚动,而是每个步骤都是动态的,上下移动。如何改变我的代码来实现这一转变


步骤
在输入数据点之间显式绘制步骤。它永远无法描绘出一个局部的“步骤”

你想要一个中间有“部分步骤”的动画

使用
ax.plot
,而不是使用
ax.step
,而是通过绘制
y=y-y%步长来制作阶梯序列

换句话说,类似于:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 1000) # Using a series of 1000 points...
y = np.sin(x)

# Make *y* increment in steps of 0.3
y -= y % 0.3

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
注意开头和结尾的部分“步骤”

将此合并到动画示例中,我们将得到类似的结果:

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([], [])

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

def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    y -= y % 0.3
    line.set_data(x, y)
    return line,

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

plt.show()

我对你想要改变的东西有点困惑。你是说你希望x轴的值增加,这样滚动就更清晰了吗?@seaotternerd是的,我想这就是我想要的。目前,这些步骤看起来就像是在原地上下移动,没有滚动。有没有办法使这些步骤间隔均匀?