【问题标题】:Plotting animation time绘制动画时间
【发布时间】:2017-02-03 15:02:17
【问题描述】:

我正在绘制动画,我想在观看动画时查看动画的时间/步骤。

fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)

cax1 = ax1.matshow(coherence_matrices[0], cmap='YlOrRd')
time = ax1.annotate(0, xy=(1, 8), xytext=(1, 8))

def animate(i):

    cax1.set_array(coherence_matrices[i])

    time.set_text(i)

    return time, cax1

anim = animation.FuncAnimation(fig, animate,
                               frames=int((4000-window_size)/window_step), interval=80, blit=True)

plt.show()

我只是想出了这个解决方案,它应该使直方图顶部出现迭代次数,但什么也没有出现。我想知道哪里出了问题,并且有一种更简单的方法可以查看动画中的计时器。

非常感谢。

【问题讨论】:

    标签: python python-3.x animation matplotlib plot


    【解决方案1】:

    我想您已经确定annotate 的坐标实际上不在图外,这也可能是文本未显示的原因。

    打开 blitting 时出现问题。不显示文字的效果有两种可能:
    1. 如果文本在坐标轴内,它会被 matshow 对象隐藏。
    2.如果文本在坐标轴之外,则根本不显示。

    现在有两种解决方案。

    (a) 不要使用 blitting。

    只需关闭 blitting 即可:

    anim = animation.FuncAnimation(fig, animate, ...., blit=False) 
    

    (b) 使用其他坐标轴。

    如果你真的需要使用 blitting(否则动画会变得太慢),你可以使用另一个轴来放置文本标签。

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    coherence_matrices = np.random.rand(80,3,3)
    
    fig = plt.figure()
    ax1 = fig.add_subplot(1, 1, 1)
    # add another axes at the top left corner of the figure
    axtext = fig.add_axes([0.0,0.95,0.1,0.05])
    # turn the axis labels/spines/ticks off
    axtext.axis("off")
    
    cax1 = ax1.matshow(coherence_matrices[0], cmap='YlOrRd')
    # place the text to the other axes
    time = axtext.text(0.5,0.5, str(0), ha="left", va="top")
    
    
    def animate(i):
        cax1.set_array(coherence_matrices[i])
    
        time.set_text(str(i))
    
        return cax1, time,
    
    anim = animation.FuncAnimation(fig, animate, frames=len(coherence_matrices),
                                        interval=80, blit=True)
    
    plt.show()
    

    【讨论】:

    • 感谢您的快速回答!最后,如您所说,我将计时器绘制在另一个窗口中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多