I am trying to create a wordcloud using python from a list of ingredients, some of which have more than one word in their name. I would like the wordcloud to consider those names as single elements, but I don't know how to achieve that. For example:
import wordcloud as w
import numpy as np
import matplotlib.pyplot as plt
ingredients = ['cabernet sauvignon', 'apple', 'black pepper',
'rice', 'smoked salmon',
'dried tomato', 'butter', 'mushroom', 'goat cheese']
frequencies = [55, 83, 33, 42, 19, 23, 5, 69, 1]
# Wordcloud asks for a string, and I have tried separating the terms with ',' and '~'
text = ''
for i, word in enumerate(ingredients):
text = text + frequencies[i] * (word + ',')
wordcloud = w.WordCloud(collocations = False).generate(text)
plt.imshow(wordcloud, interpolation = 'bilinear')
plt.axis("off")
plt.show()
The resulting wordcloud is the following. But, for example, I would like the term "cabernet sauvignon" to appear as only one word.
https://i.sstatic.net/yuHns.png
Create a dict
in the form {phrase: count, ...}
and use generate_from_frequencies
:
d = dict(zip(ingredients, frequencies))
wordcloud = w.WordCloud(collocations=False).generate_from_frequencies(d)
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