这应该很容易,但我却遇到了困难。基本上,我在matplotlib中有一个subplot,每次调用函数时都会在其中绘制一个hexbin图,但每次调用函数时我都会得到一个新的colorbar,所以我真正想做的是更新colorbar。不幸的是,这似乎行不通,因为colorbar附加的对象正在被subplot.hexbin重新创建。
def foo(self):
self.subplot.clear()
hb = self.subplot.hexbin(...)
if self.cb:
self.cb.update_bruteforce() # Doesn't work (hb is new)
else:
self.cb = self.figure.colorbar(hb)
我现在遇到了一个麻烦的问题,想要完全删除颜色条轴并重新创建它。不幸的是,当我删除颜色条轴时,子图轴不会重新占据空间,并且调用self.subplot.reset_position()没有产生我想要的效果。
def foo(self):
self.subplot.clear()
hb = self.subplot.hexbin(...)
if self.cb:
self.figure.delaxes(self.figure.axes[1])
del self.cb
# TODO: resize self.subplot so it fills the
# whole figure before adding the new colorbar
self.cb = self.figure.colorbar(hb)
fig.colorbar(cax=cax)
和cax.cla()
。 - MaxNoe