Interactive wordcloud with tooltips in Python Jupyter
Asked Answered
E

1

5

I have a list of words and phrases together with as score and a definition for each. I would like to present this as an interactive wordcloud where the text sizes are determined by the scores and the definitions appear as tooltips on hover. I would prefer to do this in Jupyter.

I know a number libraries that offer nice ways to generate wordclouds and/or tooltips. How I attach the tooltips to the words in the wordcloud?. The wordcloud needs to have a way of knowing what text you are hovering over and trigger the corresponding tooltip. I have not found a way to do that so far.

I am fairly agnostic regarding the linraries used to do this. I mainly want the result to be fairly high-level and mostly declarative. I have looked at Vega, bqplot and Andreas Mueller's wordcloud package. Vega has both wordcloud and tooltip functionality and is designed to compose piplines nicely, but I am not sure how to connect them the right way. I would also prefer to write actual Python code rather than code using JSON though, but that is a minor concern. Bqplot does tootips very nicely but does not have a wordcloud component. The wordcloud package generates nice wordclouds but I do not know how to make them interactive.

Elliellicott answered 4/3, 2018 at 0:46 Comment(2)
What's the question here? Library recommendations are off topic for Stack Overflow.Ot
@HåkenLid The question is in the ftitle & first paragrapgh: How to build an interactive wordcloud with tooltips in Jupyter? I am not asking for which liberaries to useI, but how to use them to do this. I am just mentioning what I have looked into.Elliellicott
E
6

I have done this using both ipyvega and brunel brunel is much simpler but I do not like its wordcloud layout.

Brunel

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)
Elliellicott answered 9/4, 2018 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.