Creating wordcloud using python

狂风中的少年 提交于 2019-12-02 03:13:45

First download this file Symbola.ttf in the current folder of the following script.

Architecture file:

file.txt Symbola.ttf my_word_cloud.py

file.txt:

foo buzz bizz foo buzz bizz foo buzz bizz foo buzz bizz foo buzz bizz
foo foo foo foo foo foo foo foo foo foo bizz bizz bizz bizz foo foo

my_word_cloud.py:

import io
from collections import Counter
from os import path

import matplotlib.pyplot as plt
from wordcloud import WordCloud

d = path.dirname(__file__)

# It is important to use io.open to correctly load the file as UTF-8
text = io.open(path.join(d, 'file.txt')).read()

words = text.split()
print(Counter(words))

# Generate a word cloud image
# The Symbola font includes most emoji
font_path = path.join(d, 'Symbola.ttf')
word_cloud = WordCloud(font_path=font_path).generate(text)

# Display the generated image:
plt.imshow(word_cloud)
plt.axis("off")
plt.show()

Result:

Counter({'foo': 17, 'bizz': 9, 'buzz': 5})

See a lot of other examples, here I created a simple example for you:

https://github.com/amueller/word_cloud/tree/master/examples

Greg E

most_common(x) is not a method of WordCloud. However, you can pass the parameter

max_words = 

and this should do what you're attempting.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!