Python Jupyter 中带有工具提示的交互式 wordcloud

Dan*_*ler 5 visualization data-visualization tooltip vega jupyter

我有一个单词和短语列表,以及每个单词和短语的分数和定义。我想将此作为交互式 wordcloud 呈现,其中文本大小由分数决定,定义显示为悬停时的工具提示。我更愿意在 Jupyter 中执行此操作。

我知道一些库提供了很好的方法来生成 wordcloud 和/或工具提示。如何将工具提示附加到 wordcloud 中的单词?. wordcloud 需要有办法知道你悬停在什么文本上并触发相应的工具提示。到目前为止,我还没有找到一种方法来做到这一点。

我对用于执行此操作的 linraries 相当不可知。我主要希望结果是相当高级的并且主要是声明性的。我看过 Vega、bqplot 和 Andreas Mueller 的 wordcloud 包。Vega 具有 wordcloud 和工具提示功能,旨在很好地组合管道,但我不确定如何以正确的方式连接它们。我也更愿意编写实际的 Python 代码而不是使用 JSON 的代码,但这只是一个小问题。Bqplot 很好地处理了提示信息,但没有 wordcloud 组件。wordcloud 包生成了不错的 wordcloud,但我不知道如何使它们具有交互性。

Dan*_*ler 6

我已经使用两者完成了这项工作,ipyvegabrunelbrunel 更简单,但我不喜欢它的 wordcloud 布局。

布鲁内尔

df = pd.DataFrame(data, columns=['word', 'size', 'text'])
%brunel cloud size(size) label(word) tooltip(text)
Run Code Online (Sandbox Code Playgroud)

伊皮维加

spec = {
  "$schema": "https://vega.github.io/schema/vega/v3.json",
  "name": "wordcloud",
  "width": width,
  "height": height,
  "padding": 0,
  "data" : [
      {
          'name' : 'table',
          'values' : [{'word': word, 'text': text, 'size': size}
                      for word, text size  in data]
      }
  ],
  "scales": [
    {
      "name": "color",
      "type": "ordinal",
      "range": ["#d5a928", "#652c90", "#939597"]
    }
  ],
  "marks": [
    {
      "type": "text",
      "from": {"data": "table"},
      "encode": {
        "enter": {
          "text": {"field": "word"},
          "align": {"value": "center"},
          "baseline": {"value": "alphabetic"},
          "fill": {"scale": "color", "field": "word"},
          "tooltip": {"field": "text", "type": "nominal"}
        },
        "update": {
          "fillOpacity": {"value": 1}
        },
      },
      "transform": [
        {
          "type": "wordcloud",
          "size": [width, height],
          "text": {"field": "text"},
          "font": "Helvetica Neue, Arial",
          "fontSize": {"field": "datum.size"},
        }
      ]
    }
  ],
}
Vega(spec)
Run Code Online (Sandbox Code Playgroud)