How to make mpld3 work with seaborn (interactive tooltips)

前端 未结 3 680
不知归路
不知归路 2021-01-15 08:15

I can\'t seem to get the interactive tooltips powered by mpld3 to work with the fantastic lmplot-like scatter plots from seaborn.

I\'d love any pointer on how to get

3条回答
  •  -上瘾入骨i
    2021-01-15 08:34

    Your code works for me on ipython (no notepad) when saving the figure to file with mpld3.save_html(fig,"./out.html"). May be an issue with ipython notepad/mpld3 compatibility or mpld3.display (which causes an error for me, although I think this is related to an old version of matplotlib on my computer).

    The full code which worked for me is,

    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")
    

提交回复
热议问题