在 ipython 笔记本中维护内联图的同时将 mpld3 绘图包含到库中的最简洁方法?

Cleanest way to include mpld3 plotting into libary while maintaining inline plots in ipython notebooks?

我正在编写一个更个人化的库,想要一些 class 例程,应该像这样工作:

Dog.showImage('MyDog')

这应该会立即在我的笔记本中绘制一个内联图形。 只要我不使用插件,它就可以正常工作,但是当我在库中使用鼠标位置插件和 class 例程时,到目前为止我发现的唯一方法是:

def showImage(arg1)
      f=plt.figure()
      plt.imshow(self.ImageDictionary[arg1])
      plugins.connect(f, plugins.MousePosition(fontsize=14))
      return f

获取返回的句柄,然后在笔记本中调用mpld3.display(f)。这是最好的方法吗?

谢谢

我认为给 mpld3.enable_notebook() 打电话很适合你。我通常把它放在笔记本的开头,例如

import matplotlib.pyplot as plt, mpld3
%matplotlib inline
mpld3.enable_notebook()

有了这个图案,你就不应该return这个数字了。如果这样做,它将在笔记本中出现两次。 Here is an example.