remove colorbar from figure in matplotlib

后端 未结 9 574
终归单人心
终归单人心 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:51

    Alright, here's my solution. Not terribly elegant, but not a terrible hack either.

    def foo(self):
       self.subplot.clear()
       hb = self.subplot.hexbin(...)
       if self.cb:
          self.figure.delaxes(self.figure.axes[1])
          self.figure.subplots_adjust(right=0.90)  #default right padding
       self.cb = self.figure.colorbar(hb)
    

    This works for my needs since I only ever have a single subplot. People who run into the same problem when using multiple subplots or when drawing the colorbar in a different position will need to tweak.

提交回复
热议问题