(编辑:jimmy 日期: 2024/12/27 浏览:2)
有时候我们会根据具体的场景来结合图片展示词云,比如我分析的是美团评论,那么最好的展示方法就是利用美团的logo来做词云图的底图展示,下面我们就介绍几种常用的方法!
from wordcloud import WordCloud import jieba import matplotlib.pyplot as plt import numpy as np import PIL.Image as Image text = open(u'data.txt','r',encoding='utf-8').read() stopwords={'你','我','自己','的','因为','就','他','和','是' } # 去掉无用的词 word_cut=jieba.cut(text) word_cut_join=" ".join(word_cut) mask_img=np.array(Image.open("1.jpg")) wordcloud = WordCloud( font_path='simsun.ttc',#设置字体 max_words=1000,#词云显示的最大词数 mask=mask_img,#设置背景图片 stopwords=stopwords, background_color='white'#背景颜色 ).generate(word_cut_join) plt.imshow(wordcloud) plt.axis("off") plt.show()
from wordcloud import WordCloud import jieba import numpy import PIL.Image as Image #1.将字符串切分 def chinese_jieba(text): wordlist_jieba=jieba.cut(text) space_wordlist=" ".join(wordlist_jieba) return space_wordlist with open("data.txt" ,encoding="utf-8")as file: text=file.read() text=chinese_jieba(text) #2.图片遮罩层 mask_pic=numpy.array(Image.open("1.jpg")) #3.将参数mask设值为:mask_pic wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf",mask=mask_pic).generate(text) image=wordcloud.to_image() image.show()
程序运行成功之后它会自动弹出一张照片,这个是电脑打开的,高清图片
自定义炫酷字体和配色的词云图(一键化智能!!!!)