【问题标题】:Interactive wordcloud with tooltips in Python JupyterPython Jupyter 中带有工具提示的交互式 wordcloud
【发布时间】:2018-08-11 22:45:13
【问题描述】:

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

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

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

【问题讨论】:

  • 这里有什么问题?图书馆推荐与 Stack Overflow 无关。
  • @HåkenLid 问题在标题和第一段中:如何在 Jupyter 中使用工具提示构建交互式 wordcloud?我不是要使用哪些库,而是要如何使用它们来做到这一点。我只是提到我所调查的内容。

标签: tooltip visualization data-visualization jupyter vega


【解决方案1】:

我已经使用ipyvegabrunel 完成了这项工作,brunel 简单得多,但我不喜欢它的 wordcloud 布局。

布鲁内尔

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

ipyvega

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)

【讨论】:

    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2021-02-17
    相关资源
    最近更新 更多