Animate points with labels with matplotlib

后端 未结 3 1018
时光说笑
时光说笑 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

    I think I figured out how to animate multiple annotations through a list. First you just create your annotations list:

    for i in range(0,len(someMatrix)):
         annotations.append(ax.annotate(str(i), xy=(someMatrix.item(0,i), someMatrix.item(1,i))))
    

    Then in your "animate" function you do as you have already written:

    for num, annot in enumerate(annotations):
        annot.set_position((someMatrix.item((time,num)), someMatrix.item((time,num))))
    

    (You can write it as a traditional for loop as well if you don't like the enumerate way). Don't forget to return the whole annotations list in your return statement.

    Then the important thing is to set "blit=False" in your FuncAnimation:

    animation.FuncAnimation(fig, animate, frames="yourframecount",
                              interval="yourpreferredinterval", blit=False, init_func=init)
    

    It is good to point out that blit=False might slow things down. But its unfortunately the only way I could get animation of annotations in lists to work...

提交回复
热议问题