我正在尝试从句子“嗨,你好吗”中创建一个词云。但是我只有第一个字。为什么?
代码:
#@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()
输出:
在上面的OP代码中,stopwords
参数设置为模块STOPWORDS
列表。在该列表中,都包括how
,are
,you
。这限制了这些单词无法显示在wordcloud中。
注意,如果未提供此参数,则它也将默认为该列表,因此,如果要包含所有单词,则需要加载一个空列表。
hi
,are
和you
包含在STOPWORDS
中。
[如果要保留这些特定的单词,则需要从STOPWORDS
中过滤掉它们
赞:
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()