Animate points with labels with matplotlib

后端 未结 3 1017
时光说笑
时光说笑 2020-11-30 08:34

I\'ve got an animation with lines and now I want to label the points. I tried plt.annotate() and I tried plt.text() but the labes don\'t move. This

3条回答
  •  醉酒成梦
    2020-11-30 08:49

    You have the return all objects that changed from your update function. So since your annotation changed it's position you should return it also:

    line.set_data(newData)
    annotation = plt.annotate('A0', xy=(newData[0][0],newData[1][0]))
    return line, annotation
    

    You can read more about matplotlib animations in this tutorial

    You should also specify the init function so that the FuncAnimation knows which elements to remove from the plot when redrawing on the first update. So the full example would be:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    # Create initial data
    data = np.array([[1,2,3,4,5], [7,4,9,2,3]])
    
    # Create figure and axes
    fig = plt.figure()
    ax = plt.axes(xlim=(0, 20), ylim=(0, 20))
    
    # Create initial objects
    line, = ax.plot([], [], 'r-')
    annotation = ax.annotate('A0', xy=(data[0][0], data[1][0]))
    annotation.set_animated(True)
    
    # Create the init function that returns the objects
    # that will change during the animation process
    def init():
        return line, annotation
    
    # Create the update function that returns all the
    # objects that have changed
    def update(num):
        newData = np.array([[1 + num, 2 + num / 2, 3, 4 - num / 4, 5 + num],
                            [7, 4, 9 + num / 3, 2, 3]])
        line.set_data(newData)
        # This is not working i 1.2.1
        # annotation.set_position((newData[0][0], newData[1][0]))
        annotation.xytext = (newData[0][0], newData[1][0])
        return line, annotation
    
    anim = animation.FuncAnimation(fig, update, frames=25, init_func=init,
                                   interval=200, blit=True)
    plt.show()
    

提交回复
热议问题