Python基于wordcloud及jieba實(shí)現(xiàn)中國(guó)地圖詞云圖
熱詞圖很酷炫,也非常適合熱點(diǎn)事件,抓住重點(diǎn),以圖文結(jié)合的方式表現(xiàn)出來,很有沖擊力。下面這段代碼是制作熱詞圖的,用到了以下技術(shù):
jieba,把文本分詞
wordcloud,制作熱圖
chardet,辨別文件的編碼格式,其中中文統(tǒng)一為GB18030,更加的兼容
imageio,提取圖片的形狀
其他:自動(dòng)識(shí)別文件編碼,自動(dòng)識(shí)別txt文件,圖片文件名與txt文件一致,使用的是四大名著的文本(自行百度),部分中國(guó)地圖
上代碼:
import osimport jiebaimport wordcloudimport chardetimport imageio directory = 'D:'mask = imageio.imread(r'D:map.jpg') # 用于最后圖像圖形 directory_lists = os.scandir(directory)for directory_list in directory_lists: if directory_list.is_dir() or directory_list.path.split(’.’)[-1] != 'txt': continue with open(directory_list.path, ’rb’) as fd: coding = chardet.detect(fd.read()[:1000])[’encoding’] if coding.upper() == ’GB2312’ or coding == ’GBK’: coding = ’GB18030’ file = open(directory_list.path, ’r’, encoding=coding) text = file.read() file.close() jieba_text = ’ ’.join(jieba.lcut(text)) w = wordcloud.WordCloud(height=800, width=1600, font_path=’msyh.ttc’, background_color=’white’, stopwords={’Page’}, mask=mask) w.generate(jieba_text) w.to_file(’{}.png’.format(directory_list.path.split(’.’)[0]))
輸出:
水滸傳的如下
西游記的如下
仔細(xì)看輸出的內(nèi)容,還是挺有意思的,哈哈哈。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python excel和yaml文件的讀取封裝2. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問題及解決3. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊4. Python中內(nèi)建模塊collections如何使用5. Android Studio 2.0 功能介紹6. idea重置默認(rèn)配置的方法步驟7. Android組件化和插件化開發(fā)8. Python內(nèi)存映射文件讀寫方式9. asp批量添加修改刪除操作示例代碼10. .net6 在中標(biāo)麒麟下的安裝和部署過程
