我有一个文件,其中包含一个单词及其出现的频率。我想产生一种情节;我正在寻找一种类似图的“泡沫”。想法是这些气泡的大小对应于相对频率,并且在这些气泡上标记了相应的单词。有谁知道这可以用标准的matplotlib或类似的东西完成吗?
答案 0 :(得分:2)
there中有很多库。
中的一个例子#!/usr/bin/env python
"""
Minimal Example
===============
Generating a square wordcloud from the US constitution using default arguments.
"""
import os
from os import path
from wordcloud import WordCloud
# using word frequency list:
#word_freq = open("/tmp/word_freq.txt").read()
# say it looks like this:
word_freq = {'apple': 4, 'banana': 1, 'melon': 2, 'strawberry': 3, 'grape': 8}
text = " ".join([(k + " ")*v for k,v in word_freq.items()])
# 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()
答案 1 :(得分:0)
假设您在dict data
中有频率数据
下面的代码应该可以工作
导入操作系统
from os import path
from wordcloud import WordCloud
import matplotlib.pyplot as plt
data = {
'Bla': 10,
'Bl': 2,
'cold' : 9,
'random': 6
}
wordcloud = WordCloud(max_font_size=40).generate(" ".join([(k + ' ') * v for k,v in data.items()]))
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()