How to make mpld3 work with seaborn (interactive tooltips)

前端 未结 3 679
不知归路
不知归路 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条回答
  • 2021-01-15 08:28

    I was able to get the tooltips to work by using the standard matplotlib scatter on top of the seaborn plot and very low alpha (you can't use zero)

    data_tip_points = ax.scatter(x_points, y_points, alpha=0.001)
    tooltip = plugins.PointLabelTooltip(data_tip_points, labels)
    

    It's a bit of a hack, but it works as seen here.

    http://nbviewer.ipython.org/urls/bitbucket.org/jeff_mcgehee/cds_presentation_intro/raw/49cc7808ec26adebec94ffa83973bb5db13017d7/CDS%20Intro%20Presentation.ipynb

    0 讨论(0)
  • 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")
    
    0 讨论(0)
  • 2021-01-15 08:46

    I don't think that there is an easy way to do this currently. I can get some of the tooltips to show by replacing your tooltip constructor with the following:

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

    This only works for the points outside of the uncertainty interval, though. I think it would be possible to extend seaborn to make these points highest in the zorder and store them in in the instance somewhere so that you don't need do pull them out of the axis children list. Perhaps worth a feature request.

    0 讨论(0)
提交回复
热议问题