Remove annotation while keeping plot matplotlib

后端 未结 1 849
后悔当初
后悔当初 2021-02-08 21:15

I\'m producing a series of scatterplots, where I keep most of the plot (besides the scatter plot) between each plot. This is done like so: Keeping map overlay between plots in m

相关标签:
1条回答
  • 2021-02-08 21:32

    You can remove an artist using remove().

    ann = plt.annotate (...)
    ann.remove()
    

    After removal it may be necessary to redraw the canvas.


    Here is a complete example, removing several annotations within an animation:

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.animation
    
    fig, ax = plt.subplots()
    
    x = np.arange(0, 2*np.pi, 0.01)
    f = lambda x: np.sin(x)
    line, = ax.plot(x, f(x))
    
    scat = plt.scatter([], [],  s=20, alpha=1, color="purple", edgecolors='none')
    ann_list = []
    
    def animate(j):
        for i, a in enumerate(ann_list):
            a.remove()
        ann_list[:] = []
    
        n = np.random.rand(5)*6
        scat.set_offsets([(r, f(r)) for r in n])
        for j in range(len(n)):
            ann = plt.annotate("{:.2f}".format(n[j]), xy = (n[j],f(n[j])), color = "purple", fontsize = 12)
            ann_list.append(ann)
    
    ani = matplotlib.animation.FuncAnimation(fig, animate, frames=20, interval=360)
    ani.save(__file__+".gif",writer='imagemagick', fps=3)
    plt.show()
    

    0 讨论(0)
提交回复
热议问题