这篇文章主要介绍了Python如何实现词频云图,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
首先通过pandas库读取excel文件,将读取的内容暂时保存起来。
通过wordcloud库的构造方法,设置相关的云图参数,通过方法generate,生成云图。
import hashlib
import pandas as pd
from wordcloud import WordCloud
geo_data = pd.read_excel(r'geo_data.xlsx')
#筛选出非空列表值
words = ','.join(x for x in geo_data['city'] if x!= [])
wc = WordCloud(
#背景颜色设为绿色
background_color = 'green',
#显示最大词数
max_words=100,
#显示中文
font_path='.fonts/simhei.ttf',
min_font_size=5,
max_font_size=100,
#图幅宽度
width=500
)
x=wc.generate(words)
x.to_file('geo_data.png')
import hashlibimport pandas as pdfrom wordcloud import WordCloud
然后通过read_excel函数读取excel中的数据,数值存入变量words;
通过WordCloud构造词云参数,包括颜色、最大词数、字体大小限制、图幅的宽度等等;to_file方法,将生成的云图保存到当前目录,名称为geo_data.png
感谢你能够认真阅读完这篇文章,希望小编分享的“Python如何实现词频云图”这篇文章对大家有帮助,同时也希望大家多多支持天达云,关注天达云行业资讯频道,更多相关知识等着你来学习!