remove colorbar from figure in matplotlib

后端 未结 9 573
终归单人心
终归单人心 2020-12-08 14:14

This should be easy but I\'m having a hard time with it. Basically, I have a subplot in matplotlib that I\'m drawing a hexbin plot in every time a function is called, but ev

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 14:46

    I think the problem is that with del you cancel the variable, but not the referenced object colorbar. If you want the colorbar to be removed from plot and disappear, you have to use the method remove of the colorbar instance and to do this you need to have the colorbar in a variable, for which you have two options:

    1. holding the colorbar in a value at the moment of creation, as shown in other answers e.g. cb=plt.colorbar()
    2. retrieve an existing colorbar, that you can do following (and upvoting :)) what I wrote here: How to retrieve colorbar instance from figure in matplotlib then:

    cb.remove() plt.draw() #update plot

提交回复
热议问题