Montana Burr
Montana Burr

Reputation: 287

My word cloud is missing three out of the four words in my string. How do I add the words it's missing?

I am trying to create a word cloud from the sentence, "hi how are you". But I only get the first word. Why?

Code:

#@title Bar plot of most frequent words.
from wordcloud import WordCloud,STOPWORDS
stopwords = set(STOPWORDS)
wordcloud = WordCloud(
    width=800,height=800,
    stopwords = stopwords,
    min_font_size = 10,
    background_color='white'
).generate("hi how are you") 
# plot the WordCloud image                        
plt.figure(figsize = (8, 8), facecolor = None) 
plt.imshow(wordcloud,interpolation="bilinear") 
plt.axis("off") 
plt.tight_layout(pad = 0) 

plt.show()

Output:

enter image description here

Upvotes: 2

Views: 1018

Answers (2)

emartinelli
emartinelli

Reputation: 1047

hi, are and you are contained in STOPWORDS.

If you want to keep these specific words you need to filter out them from STOPWORDS

Like:

from wordcloud import WordCloud, STOPWORDS

stopwords = {word for word in STOPWORDS if word not in {'how', 'are', 'you'}}
wordcloud = WordCloud(
    width=800,height=800,
    stopwords = stopwords,
    min_font_size = 10,
    background_color='white'
).generate("hi how are you")

# plot the WordCloud image           
plt.figure(figsize = (8, 8), facecolor=None)
plt.imshow(wordcloud,interpolation="bilinear")
plt.axis("off")
plt.tight_layout(pad = 0)

plt.show()

Upvotes: 0

user1558604
user1558604

Reputation: 987

In the OP code above, stopwords argument is set to the module STOPWORDS list. In this list, how, are, you are all included. This restricts these words from showing in the wordcloud.

Note, if this argument is not given, it will also default to this list, so you will need to load in an empty list if you want all words included.

Upvotes: 2

Related Questions