Search code examples
pythonpython-3.xdata-visualizationinformation-retrievalword-cloud

Keeping words together in WordCloud


I'm working with word clouds in python, using the wordcloud library.

As an example, I want to do a wordcloud from the following list:

word_ls = ['orchards growers northern', 'apple orchards growers', 'threatening apple orchards']

The issue I'm facing is that when I generate the cloud, I can't have it consider each string individually, instead of word by word

I have tried doing the token separation differently using regexp property, though unsuccessfully (getting KeyError, with r"\w[\w ']+")

Any insights?

sample wordcloud generation snippet:

word_text = ";".join(word_ls)
wordcloud = WordCloud().generate(word_text)
wordcloud.to_file("word_test.png")

Solution

  • That should work

    from wordcloud import WordCloud
    from collections import Counter
    
    word_ls = ['orchards growers northern', 'apple orchards growers', 'threatening apple orchards']
    word_could_dict = Counter(word_ls)
    wordcloud = WordCloud().generate_from_frequencies(word_could_dict)
    wordcloud.to_file("word_test.png")
    

    enter image description here