from wordcloud import WordCloud, ImageColorGenerator
import matplotlib.pyplot as plt
def random_color_func(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None):
return (0,122,207)
# 生成词云
def create_word_cloud():
frequencies = {}
for line in open("康师傅总体数据-不喜欢.txt", encoding='utf-8'):
arr = line.split(" ")
frequencies[arr[0]] = float(arr[1])
# 支持中文, SimHei.ttf可从以下地址下载:https://github.com/cystanford/word_cloud
background_Image = plt.imread('ksf11.jpg')
wc = WordCloud(
background_color='white',
font_path="./msyh.ttc",
max_words=50,
width=2000,
height=1200,
color_func=random_color_func,
prefer_horizontal=1,
)
word_cloud = wc.generate_from_frequencies(frequencies)
# 写词云图片
word_cloud.to_file("康师傅总体数据-不喜欢.jpg")
# 显示词云文件
plt.imshow(word_cloud)
plt.axis("off")
plt.show()
# 根据词频生成词云
create_word_cloud()
python使用wordcloud生成词云图
最新推荐文章于 2024-06-04 06:56:40 发布