Matplotlib - How to remove a specific line or curve

后端 未结 5 1099
栀梦
栀梦 2020-12-06 01:21

I want to remove a specific line in a plot of multiple lines. Bellow is a given example which is not sufficient for me because it removes only the last plotted line and not

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 02:05

    You can also use this for multiple subplots

    subfig, subax = plt.subplots(3) 
    
    def add_series(x, y0, y1, y2, gid):
        plt.figure(subfig.number)
        ln, = subax[0].plot(x, y0, gid=gid)
        ln, = subax[1].plot(x, y1, gid=gid)
        ln, = subax[2].plot(x, y2, gid=gid)
        plt.draw()
    
    def remove_series(self, gid):
        plt.figure(subfig.number)
        for c0, c1, c2 in zip(subax[0].lines, subax[1].lines, subax[2].lines):
            if c0.get_gid() == gid:
                c0.remove()
            if c1.get_gid() == gid:
                c1.remove()
            if c2.get_gid() == gid:
                c2.remove()
        plt.draw()
    

提交回复
热议问题