使用频率字典绘制wordcloud

时间:2019-05-24 13:31:07

标签: python plot word-cloud

我想绘制一个这样的词云:

example wordcloud

我的字数词典:

dic = {word1:14, word2:242, word3:343}

1 个答案:

答案 0 :(得分:0)

您可以为此使用word cloud库。也可以从命令行使用它,显然您需要查看文档以使其提供所需的输出。

请查看以下answer作为示例,或从网站上查找以下示例:

Minimal Example
===============
Generating a square wordcloud from the US constitution using default arguments.
"""

import os

from os import path
from wordcloud import WordCloud

# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()

# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()

# Generate a word cloud image
wordcloud = WordCloud().generate(text)

# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")

# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()

# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()
# image.show()