Matplotlib - How to remove a specific line or curve

后端 未结 5 1093
栀梦
栀梦 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 01:55

    I had the same need and for me, it turned out to be tidier to add an id to the data series and remove it later by finding the series (collection) with the given id.

    def add_series(x, id):
      plt.plot(x, gid = id)
    
    def remove_series(id):
      for c in plt.collections: # possibly better to use: for c in plt.lines (see comment)
        if c.get_gid() == id:
          c.remove()
    

提交回复
热议问题