Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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 - Fatal编程技术网

Python 如何在matplotlib中调整暂停动画的坐标范围?

Python 如何在matplotlib中调整暂停动画的坐标范围?,python,animation,matplotlib,Python,Animation,Matplotlib,我希望能够在动画正在运行或处于停止状态时更改动画绘图的xlim和ylim。在动画运行时更改它们(使用ax.set_xlim/set_ylim调用)效果良好,但如果使用event_source.stop()暂停动画,则沿坐标轴绘制的数字保持不变 下面是显示此问题的测试程序。尝试在动画运行时按“-”和“+”键,您将看到蓝色对象已缩放,坐标范围已更新。但是,如果先按空格键暂停动画,然后按“-”或“+”,则仅缩放蓝色对象,但坐标范围保持不变(并且动画作为调用ani的副作用而恢复。_end_redraw(

我希望能够在动画正在运行或处于停止状态时更改动画绘图的xlim和ylim。在动画运行时更改它们(使用ax.set_xlim/set_ylim调用)效果良好,但如果使用event_source.stop()暂停动画,则沿坐标轴绘制的数字保持不变

下面是显示此问题的测试程序。尝试在动画运行时按“-”和“+”键,您将看到蓝色对象已缩放,坐标范围已更新。但是,如果先按空格键暂停动画,然后按“-”或“+”,则仅缩放蓝色对象,但坐标范围保持不变(并且动画作为调用ani的副作用而恢复。_end_redraw(无))


应重新绘制整个画布,以确保可观察到对限制的更改。我想一个好的策略应该是1。改变限制,2。绘制画布,3.停止动画,抓取新背景(通过
\u handle\u resize
),4。重新启动动画(通过
\u end\u redraw


同样,这仅仅是使用闪电的一个问题。所以这个要求应该是问题的一部分。谢谢你,是的,它有效!你又一次帮助了我。我希望有一天我能报答你的好意!:)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from numpy import sin, cos, pi

def keypress(event):
    global anim_running
    if event.key == ' ':
        ani.event_source.stop() if anim_running else ani.event_source.start()
        anim_running = not anim_running
    elif event.key == '+':
        ax.set_xlim([-4,4])
        ax.set_ylim([-4,4])
        ani._end_redraw(None)
    elif event.key == '-':
        ax.set_xlim([-1,1])
        ax.set_ylim([-1,1])
        ani._handle_resize()
        ani._end_redraw(None)

phi = pi/2

def animate(i):
    global phi
    line.set_data([[0.0, sin(phi)], [0.0, cos(phi)]])
    phi += 0.01
    return line,

fig,ax = plt.subplots(1, 1)
ax.set_xlim([-2,2])
ax.set_ylim([-2,2])
line, = ax.plot([], [], 'o-', lw=2, color='b')
fig.canvas.mpl_connect('key_press_event', keypress)
ani = animation.FuncAnimation(fig, animate, blit=True, interval=0, frames=2000)
anim_running = True
plt.show()
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from numpy import sin, cos, pi

def keypress(event):
    global anim_running
    if event.key == ' ':
        ani.event_source.stop() if anim_running else ani.event_source.start()
        anim_running = not anim_running
    elif event.key == '+':
        ax.set_xlim([-4,4])
        ax.set_ylim([-4,4])
        fig.canvas.draw()
        ani._handle_resize()
        ani._end_redraw(None)
    elif event.key == '-':
        ax.set_xlim([-1,1])
        ax.set_ylim([-1,1])
        fig.canvas.draw()
        ani._handle_resize()
        ani._end_redraw(None)

phi = pi/2

def animate(i):
    global phi
    line.set_data([[0.0, sin(phi)], [0.0, cos(phi)]])
    phi += 0.01
    return line,

fig,ax = plt.subplots(1, 1)
ax.set_xlim([-2,2])
ax.set_ylim([-2,2])
line, = ax.plot([], [], 'o-', lw=2, color='b')
fig.canvas.mpl_connect('key_press_event', keypress)
ani = animation.FuncAnimation(fig, animate, blit=True, interval=2, frames=2000)
anim_running = True
plt.show()