这应该很容易,但我很难做到。基本上,我在 matplotlib 中有一个子图,每次调用函数时我都会绘制一个 hexbin 图,但是每次调用该函数时,我都会得到一个新的颜色条,所以我真正想做的是更新颜色条. 不幸的是,这似乎不起作用,因为颜色条附加到的对象正在由 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)
有没有人有什么建议?
非常感激!亚当