How to make mpld3 work with seaborn (interactive tooltips)

二次信任 提交于 2019-12-01 13:09:16

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.

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

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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!