在python中为列表中的项生成词云

时间:2017-08-09 10:55:59

标签: python string list word word-cloud

 my_list=["one", "one two", "three"]

我正在使用

生成此列表的文字云
 wordcloud = WordCloud(width = 1000, height = 500).generate(" ".join(my_list))

当我将所有项目转换为字符串时,它正在为

生成文字云
   "one","two","three"

 But I want to generate word cloud for the values, "one","one two","three"

帮助我为列表中的项目生成文字云

1 个答案:

答案 0 :(得分:6)

一种做法,

import matplotlib.pyplot as plt

#convert list to string and generate
unique_string=(" ").join(my_list)
wordcloud = WordCloud(width = 1000, height = 500).generate(unique_string)
plt.figure(figsize=(15,8))
plt.imshow(wordcloud)
plt.axis("off")
plt.savefig("your_file_name"+".png", bbox_inches='tight')
plt.show()
plt.close()

创建Counter Dictionary的另一种方法,

#convert it to dictionary with values and its occurences
from collections import Counter
word_could_dict=Counter(my_list)
wordcloud = WordCloud(width = 1000, height = 500).generate_from_frequencies(word_could_dict)

plt.figure(figsize=(15,8))
plt.imshow(wordcloud)
plt.axis("off")
#plt.show()
plt.savefig('yourfile.png', bbox_inches='tight')
plt.close()