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.
I have done this using both ipyvega
and brunel
brunel is much simpler but I do not like its wordcloud layout.
df = pd.DataFrame(data, columns=['word', 'size', 'text'])
%brunel cloud size(size) label(word) tooltip(text)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With