当前位置: 首页 > 后端技术 > Python

Python生成词云图_0

时间:2023-03-26 13:30:56 Python

代码如下:fromosimportpathfromwordcloudimportWordCloudimportmatplotlibmatplotlib.use('TkAgg')#获取目录地址,读取文本d=path.dirname(__file__)text=open(path.join(d,'haha.txt').read()#生成词云图wordcloud=WordCloud().generate(text)#pil方式显示生成的词云图image=wordcloud.to_image()image.show()fromosimportpathfromPILimportImageimportnumpyasnpiportmatplotlibmatplotlib.use('TkAgg')fromwordcloudimportWordCloud,STOPWORDSd=path.dirname(__file__)#readthewholetext.text=open(path.join(d),'haha.txt')).read()读取图片alice_mask=np.array(Image.open(path.join(d,"heart.png")))添加停用词stopwords=set(STOPWORDS)stopwords.add("哈哈")设置词云的一些属性外汇经纪商比较http://www.fx61.com/brokerlistwc=WordCloud(background_color="black",max_words=2000,mask=alice_mask,stopwords=stopwords)#生成w奥德云厕所。generate(text)#pil方式显示生成的词云图(如果你没有matplotlib)image=wc.to_image()image.show()fromosimportpathfromPILimportImageimportnumpyasnpimportmatplotlibmatplotlib.use('TkAgg')importmatplotlib.pyplotaspltfromwordcloudimportWordCloud,STOPWORDSimportjiebad=path.dirname(__file__)##读取整个text.text=open(path.join(d,'Chinese.txt')).read()#打印文本##读取图片alice_mask=np.array(Image.open(path.join(d,"dog.jpeg")))stopwords_path='stopwords.txt'#停止词列表my_words_list=['donothing']#添加口吃词库中的新词#添加自己的词库分词defadd_word(list):foritemsinlist:jieba.add_word(items)add_word(my_words_list)#口吃wordlist=jieba.cut(text,cut_all=False)wl="".join(wordlist)#print(wl)#分词后输出txt#去掉停用词mywordlist=[]f_stop=open(stopwords_path)try:f_stop_text=f_stop.read()f_stop_text=unicode(f_stop_text,'utf-8')#printf_stop_textfinally:f_stop.close()f_stop_seg_list=f_stop_text.split('\n')formywordinwl.split(""):forstopwordinf_stop_seg_list:if(myword.strip()==stopword)或len(myword.strip())<=1:break;else:mywordlist.append(myword)mywordlist="".join(mywordlist)#stopwords=set(STOPWORDS)#stopwords.add("likethis")#设置词云wc=WordCloud(background_color="black",#设置背景颜色mask=alice_mask,#设置背景图片max_words=2000,#设置最大显示字数#font_path="fangsong_GB2312.ttf",font_path="ChineseSongStyle.ttf",max_font_size=50,#设置字体最大值random_state=30,#设置多少个随机生成状态,即有多少种配色方案)#wc=WordCloud(background_color="black",max_words=2000,mask=alice_mask,font_path="Chinese宋体.ttf",#stopwords=stopwords)myword=wc.generate(mywordlist)#生成词云#显示词云图plt.imshow(myword)plt.axis("off")plt.show()词云图模糊怎么解决?默认参数图片分辨率低。设置比例参数。参数越大,分辨率越高。两个字算一个字。collocations=False#对于重复关键字的问题,我们可以使用collocations参数来解决其他python显示的中文字符串列表,直接输出会以unicode编码格式显示。导入jsonprintjson.dumps(f_stop_seg_list,encoding="UTF-8",ensure_ascii=False)