Matplotlib 如何使mpld3与seaborn一起工作(交互式工具提示)

Matplotlib 如何使mpld3与seaborn一起工作(交互式工具提示),matplotlib,seaborn,mpld3,Matplotlib,Seaborn,Mpld3,我似乎无法让mpld3提供的交互式工具提示与seaborn提供的奇妙的lmplot(如散点图)配合使用 我想要任何关于如何让它工作的指针!谢谢 示例代码: # I'm running this in an ipython notebook. %matplotlib inline import matplotlib.pyplot as plt, mpld3 mpld3.enable_notebook() import seaborn as sns N=10 data = pd.DataFram

我似乎无法让mpld3提供的交互式工具提示与seaborn提供的奇妙的lmplot(如散点图)配合使用

我想要任何关于如何让它工作的指针!谢谢

示例代码:

# I'm running this in an ipython notebook.
%matplotlib inline
import matplotlib.pyplot as plt, mpld3
mpld3.enable_notebook()
import seaborn as sns


N=10
data = pd.DataFrame({"x": np.random.randn(N),
                     "y": np.random.randn(N), 
                     "size": np.random.randint(20,200, size=N),
                     "label": np.arange(N)
                     })


scatter_sns = sns.lmplot("x", "y", 
           scatter_kws={"s": data["size"]},
           robust=False, # slow if true
           data=data, size=8)
fig = plt.gcf()

tooltip = mpld3.plugins.PointLabelTooltip(fig, labels=list(data.label))
mpld3.plugins.connect(fig, tooltip)

mpld3.display(fig)
我正在获取seaborn绘图,同时出现以下错误:

Javascript error adding output!
TypeError: obj.elements is not a function
See your browser Javascript console for more details.
控制台显示:

TypeError: obj.elements is not a function
    at mpld3_TooltipPlugin.draw (https://mpld3.github.io/js/mpld3.v0.2.js:1161:9)
    at mpld3_Figure.draw (https://mpld3.github.io/js/mpld3.v0.2.js:1400:23)
    at Object.mpld3.draw_figure (https://mpld3.github.io/js/mpld3.v0.2.js:18:9)
    at eval (eval at <anonymous> (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), <anonymous>:14:14)
    at eval (eval at <anonymous> (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), <anonymous>:15:5)
    at eval (native)
    at Function.x.extend.globalEval (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231)
    at x.fn.extend.domManip (https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:21253)
    at x.fn.extend.append (https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:18822)
    at OutputArea._safe_append (https://mbcomp1:9999/static/notebook/js/outputarea.js:336:26)
outputarea.js:319 Javascript error adding output! TypeError: obj.elements is not a function
    at mpld3_TooltipPlugin.draw (https://mpld3.github.io/js/mpld3.v0.2.js:1161:9)
    at mpld3_Figure.draw (https://mpld3.github.io/js/mpld3.v0.2.js:1400:23)
    at Object.mpld3.draw_figure (https://mpld3.github.io/js/mpld3.v0.2.js:18:9)
    at eval (eval at <anonymous> (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), <anonymous>:14:14)
    at eval (eval at <anonymous> (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), <anonymous>:15:5)
    at eval (native)
    at Function.x.extend.globalEval (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231)
    at x.fn.extend.domManip (https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:21253)
    at x.fn.extend.append (https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:18822)
    at OutputArea._safe_append (https://mbcomp1:9999/static/notebook/js/outputarea.js:336:26)
outputarea.js:338 TypeError: obj.elements is not a function
    at mpld3_TooltipPlugin.draw (https://mpld3.github.io/js/mpld3.v0.2.js:1161:9)
    at mpld3_Figure.draw (https://mpld3.github.io/js/mpld3.v0.2.js:1400:23)
    at Object.mpld3.draw_figure (https://mpld3.github.io/js/mpld3.v0.2.js:18:9)
    at eval (eval at <anonymous> (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), <anonymous>:14:14)
    at eval (eval at <anonymous> (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), <anonymous>:15:5)
    at eval (native)
    at Function.x.extend.globalEval (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231)
    at x.fn.extend.domManip (https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:21253)
    at x.fn.extend.append (https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:18822)
    at OutputArea._safe_append (https://mbcomp1:9999/static/notebook/js/outputarea.js:336:26)
outputarea.js:319 Javascript error adding output! TypeError: obj.elements is not a function
    at mpld3_TooltipPlugin.draw (https://mpld3.github.io/js/mpld3.v0.2.js:1161:9)
    at mpld3_Figure.draw (https://mpld3.github.io/js/mpld3.v0.2.js:1400:23)
    at Object.mpld3.draw_figure (https://mpld3.github.io/js/mpld3.v0.2.js:18:9)
    at eval (eval at <anonymous> (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), <anonymous>:14:14)
    at eval (eval at <anonymous> (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), <anonymous>:15:5)
    at eval (native)
    at Function.x.extend.globalEval (https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231)
    at x.fn.extend.domManip (https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:21253)
    at x.fn.extend.append (https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:18822)
    at OutputArea._safe_append (https://mbcomp1:9999/static/notebook/js/outputarea.js:336:26)
TypeError:obj.elements不是函数
在mpld3_TooltipPlugin.draw(https://mpld3.github.io/js/mpld3.v0.2.js:1161:9)
在mpld3_图中绘制(https://mpld3.github.io/js/mpld3.v0.2.js:1400:23)
在Object.mpld3.draw\u图中(https://mpld3.github.io/js/mpld3.v0.2.js:18:9)
在评估时(评估时)(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), :14:14)
在评估时(评估时)(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), :15:5)
评估时(本地)
在Function.x.extend.globalEval处(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231)
在x.fn.extend.domManip(https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:21253)
在x.fn.extend.append处(https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:18822)
在输出区。\u安全\u附加(https://mbcomp1:9999/static/notebook/js/outputarea.js:336:26)
js:319JavaScript添加输出时出错!TypeError:obj.elements不是函数
在mpld3_TooltipPlugin.draw(https://mpld3.github.io/js/mpld3.v0.2.js:1161:9)
在mpld3_图中绘制(https://mpld3.github.io/js/mpld3.v0.2.js:1400:23)
在Object.mpld3.draw\u图中(https://mpld3.github.io/js/mpld3.v0.2.js:18:9)
在评估时(评估时)(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), :14:14)
在评估时(评估时)(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), :15:5)
评估时(本地)
在Function.x.extend.globalEval处(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231)
在x.fn.extend.domManip(https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:21253)
在x.fn.extend.append处(https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:18822)
在输出区。\u安全\u附加(https://mbcomp1:9999/static/notebook/js/outputarea.js:336:26)
outputarea.js:338 TypeError:obj.elements不是函数
在mpld3_TooltipPlugin.draw(https://mpld3.github.io/js/mpld3.v0.2.js:1161:9)
在mpld3_图中绘制(https://mpld3.github.io/js/mpld3.v0.2.js:1400:23)
在Object.mpld3.draw\u图中(https://mpld3.github.io/js/mpld3.v0.2.js:18:9)
在评估时(评估时)(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), :14:14)
在评估时(评估时)(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), :15:5)
评估时(本地)
在Function.x.extend.globalEval处(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231)
在x.fn.extend.domManip(https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:21253)
在x.fn.extend.append处(https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:18822)
在输出区。\u安全\u附加(https://mbcomp1:9999/static/notebook/js/outputarea.js:336:26)
js:319JavaScript添加输出时出错!TypeError:obj.elements不是函数
在mpld3_TooltipPlugin.draw(https://mpld3.github.io/js/mpld3.v0.2.js:1161:9)
在mpld3_图中绘制(https://mpld3.github.io/js/mpld3.v0.2.js:1400:23)
在Object.mpld3.draw\u图中(https://mpld3.github.io/js/mpld3.v0.2.js:18:9)
在评估时(评估时)(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), :14:14)
在评估时(评估时)(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231), :15:5)
评估时(本地)
在Function.x.extend.globalEval处(https://mbcomp1:9999/static/components/jquery/jquery.min.js:4:4231)
在x.fn.extend.domManip(https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:21253)
在x.fn.extend.append处(https://mbcomp1:9999/static/components/jquery/jquery.min.js:5:18822)
在输出区。\u安全\u附加(https://mbcomp1:9999/static/notebook/js/outputarea.js:336:26)

当使用
mpld3.save_html(图,“./out.html”)将图形保存到文件时,您的代码在
ipython
(无记事本)上对我有效。
。可能是ipython
记事本
/
mpld3
兼容性或
mpld3.display
(这会导致我出错,尽管我认为这与我计算机上旧版本的matplotlib有关)

对我有效的完整代码是

import numpy as np
import matplotlib.pyplot as plt, mpld3
import seaborn as sns
import pandas as pd

N=10
data = pd.DataFrame({"x": np.random.randn(N),
                     "y": np.random.randn(N), 
                     "size": np.random.randint(20,200, size=N),
                     "label": np.arange(N)
                     })


scatter_sns = sns.lmplot("x", "y", 
           scatter_kws={"s": data["size"]},
           robust=False, # slow if true
           data=data, size=8)
fig = plt.gcf()

tooltip = mpld3.plugins.PointLabelTooltip(fig, labels=list(data.label))
mpld3.plugins.connect(fig, tooltip)

mpld3.save_html(fig,"./out.html")

我认为目前没有一种简单的方法可以做到这一点。通过将
tooltip
构造函数替换为以下内容,我可以获得一些要显示的工具提示:

ax = plt.gca()
pts = ax.get_children()[3]
tooltip = mpld3.plugins.PointLabelTooltip(pts, labels=list(data.label))

不过,这仅适用于不确定区间以外的点。我认为可以扩展
seaborn
,使这些点在
zorder
中处于最高位置,并将它们存储在实例中的某个位置,这样您就不需要将它们从axis子项列表中拉出。也许值得一个功能请求。

我可以通过在seaborn绘图顶部使用标准matplotlib散点和非常低的alpha(不能使用零)来获得工具提示

这是一个有点黑客,但它的工作原理如图所示


不幸的是,即使在导出到html时,我也会遇到同样的错误。如果这是版本问题。我使用的是:mpld3 0.2 matplotlib 1.4.3 seaborn 0.5.1这里是。
data_tip_points = ax.scatter(x_points, y_points, alpha=0.001)
tooltip = plugins.PointLabelTooltip(data_tip_points, labels)