首先导入一下
from wordcloud import WordCloud
一,WordCloud()
wc=WordCloud(background_color="white",
repeat=True,max_words=100,
height=480,width=854,
max_font_size=200,font_path="fonts/msyh.ttc,colormap='Reds')
background_color:背景色
repeat:词组是否重复,重复就写True
max_words:词组最多出现多少次
height:画布的高
width:画布的宽
max_font_size:字体最大是多少
font_path:中文字体路径(wordcloud不支持中文,怎么解决查查其他文章吧)
colormap:词组显示的颜色,下图是各种颜色的名称
二,generate()
通过文本生成词云的方法
要事先准备好文本text,我这里讲最浅的,深的我厉害了再说
text='清华 北大 复旦 上海交大 南大 湖师大'
wc.generate(text)
三,recolor()
可以把颜色的深浅重新分配
wc.recolor()
四,to_file()保存图片,括号里写你给图片的命名
wc.to_file('test.jpg')