jdk 2019-03-26
思维导图:
效果(语句版):
源码:
# -*- coding: utf-8 -*- """ Created on Tue Mar 5 17:59:29 2019 @author: dell """ # ============================================================================= # 步骤: # 分割aaa = jieba.cut(str,cut_all=True/False) # 连接bbb = " ".join(aaa) # 制作xxx = WordCloud(background_color,font_path).generate(bbb) #bbb为字符串 # 显示plt.imshow(xxx) #不能用plt.show() # 取消坐标轴的显示Matplotlib.pyplot.axis("off") # 存为图片xxx.to_file(path) # ============================================================================= from wordcloud import WordCloud from matplotlib import pyplot as plt import jieba with open("pythonTest.txt",encoding="utf-8") as f: text = f.read() #textFromFile = open("pythonTest",encoding = "UTF-8").read() word_list = jieba.cut("ABVDEFG",cut_all=True) #切成了一个个的字符串 xxx = " ".join(word_list) #"分隔符".join(需要被连接的数据) 将内容连接为字符串 myWordCloud = WordCloud(background_color="white",font_path='C:\windows\Fonts\STZHONGS.TTF').generate(text) #myWordCloud = WordCloud(background_color="white",width=1000,height=860,font_path='C:\windows\Fonts\STZHONGS.TTF').generate(text) plt.axis("off") #plt.show(myWordCloud) #没有实际显示,只有背景!!! plt.imshow(myWordCloud) myWordCloud.to_file("词云图片.jpg") #保存为图片
注意事项:
<一> jieba分词
import jieba wordList = jieba.cut("机器学习,算法对新鲜样本!的适应能力:叫泛化能力",cut_all=False) print(type(wordList)) #类型是一个生成器generator print(wordList) #本身是一个生成器对象generator Object for list in wordList: if list in ",./;'[]~!@#$%^&*()_+,。、;‘ 【】~!@#¥%……&*()――+《 》?:“{}<>?:\n\r": None else: print(list)
<二> 对词图进行重新上色的注意事项
<三> 读取图片时候的注意事项
a = np.array(Image.open(路径))
<四> python中文件路径注意事项
<五> Spyder中的注释快捷键