Seaborn热图:使用可点击的超链接进行注释

时间:2019-01-08 18:40:48

标签: python matplotlib jupyter-notebook data-visualization seaborn

在Jupyter笔记本中是否可以使用可单击的超链接注释seaborn热图? documentation清楚地说明了如何创建简单的带注释的热图。我的问题是,是否可以使注释在Jupyter中可点击?

1 个答案:

答案 0 :(得分:0)

原则上,在Python matlplotlib add hyperlink to text中给出了在jupyter的matplotlib图形中创建可单击的超链接的答案。要将其应用于热图,需要手动添加文本。即在那种情况下,不使用seaborn来创建热图,而是直接使用matplotlib。

以下内容将创建一个带注释的热图,您可以在其中单击任意数字,然后将其定向到wolframalpha上的相应页面。

import numpy as np
import matplotlib.pyplot as plt
from IPython.display import set_matplotlib_formats
set_matplotlib_formats("svg")

data = np.random.randint(1,50, size=(3,3))

fig, ax = plt.subplots()
im=ax.imshow(data)
fig.colorbar(im)
for (j,i), x in np.ndenumerate(data):
    url = "https://www.wolframalpha.com/input/?i={}".format(x)
    ax.annotate(x, xy=(i,j), ha="center", va="center",
                url=url, bbox=dict(color='w', alpha=1e-6, url=url))

enter image description here