亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python基于WordCloud制作詞云圖

 更新時間:2019年11月29日 11:18:54   作者:落日峽谷  
這篇文章主要介紹了python基于WordCloud制作詞云圖,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了python基于WordCloud制作詞云圖,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

1. 導(dǎo)入需要的包package

import matplotlib.pyplot as plt
from scipy.misc import imread
from wordcloud import WordCloud,STOPWORDS
import xlrd

2. 設(shè)置生成詞云圖的背景圖片,最好是分辨率高且色彩邊界分明的圖片

def set_background(picpath):
  back_coloring = imread(picpath)# 設(shè)置背景圖片,png等圖片格式
  return back_coloring

3. 創(chuàng)建詞云圖:WordCloud

def create_word_cloud(txt_str, back_coloring): #txt_str表示導(dǎo)入的是字符串格式數(shù)據(jù),#back_color表示的是背景圖片位置
  print('---- 根據(jù)詞頻,開始生成詞云! ----')
  font = r'C:\Windows\Fonts\simsun.ttc' #加載顯示字體
  wc = WordCloud(
    font_path=font,
    collocations=False, # 去重,如果不加,詞云圖會顯示相同的詞
    stopwords=STOPWORDS, #加載停用詞,如果不自己指定,則會加載默認的停用詞
    max_words=100,
    width=2000,
    height=1200,
    # background_color='white',
    mask=back_coloring,
  )
  wordcloud = wc.generate(txt_str)
  # 寫詞云圖片
  wordcloud.to_file(".\wordcloud_test.png")
  # 顯示詞云文件
  plt.imshow(wordcloud)
  plt.axis("off")
  plt.show()

4. 默認的停用詞一般在:假如anaconda安裝在D盤,則會在其目錄:D:\Anaconda3\Lib\site-packages\wordcloud\stopwords,其中都是英文詞,例如:

注意:也可以在jieba分詞中,先利用自己的停用詞,得到去除停用詞之后的文本字符串來繪制詞云圖:

5. 此時,詞云圖無法顯示數(shù)字,這是因為 wc.generate 操作中,有去除數(shù)字的語句:在wordcloud.py中,第560行左右,所以想要顯示數(shù)字,需要先注釋這一行

6. 假設(shè)想要顯示的詞,已經(jīng)經(jīng)過jieba分詞,保存在txt文檔中,則繪制詞云圖的方法是:

例如:txt中是每行是一個詞:

則,先讀取txt文件,形成字符串格式文本,再繪制

if __name__ == '__main__': 
  picpath = r".\xxx.png" #背景圖片路徑
  back_coloring = set_background(picpath)
  
  with open(r".\jieba_分詞數(shù)據(jù).txt", "r",encoding='utf-8') as f:
    remove_stop_str = f.read()
  
  create_word_cloud(remove_stop_str, back_coloring)

7. 如果通過jieba分詞的數(shù)據(jù)已經(jīng)處理成了(詞, 詞頻)并保存在excel中,例如這種兩列格式的excel表,第一行是標(biāo)簽如(詞, 詞頻):

則可以先讀取詞頻再顯示,python讀取excel數(shù)據(jù)可以通過 xlrd.open_workbook 方法:

def read_from_xls(filepath,index_sheet):
  #讀取文件名,filepath是excel文件的路徑,index_sheet是第幾個sheet
  #讀取表格#
  # 設(shè)置GBK編碼
  xlrd.Book.encoding = "gbk"
  rb = xlrd.open_workbook(filepath)
  print(rb)

  sheet = rb.sheet_by_index(index_sheet)
  nrows = sheet.nrows
  data_tmp = []

  for i in range(nrows - 1):
    tt=i+1 #excel的第一行是標(biāo)簽
    tmp_char = [str(sheet.cell_value(tt,0))] #第一列是詞
    tmp_num = int(sheet.cell_value(tt,1))  #第二列是詞頻
    data_tmp.extend(tmp_char*tmp_num)
  return data_tmp

然后,讀數(shù)據(jù)和生成詞云圖:

if __name__ == '__main__': 
  picpath = r".\xxx.png"
  back_coloring = set_background(picpath)
  
  data_dic = read_from_xls(r'D:\Python_workspace\spyder_space\jieba分詞表.xlsx',0)
  data_dic_str = '\n'.join(data_dic) #轉(zhuǎn)成字符串格式
  
  create_word_cloud(data_dic_str, back_coloring)

8. 總結(jié)代碼

# -*- coding: utf-8 -*-
"""
Created on Mon Aug 19 10:47:17 2019

@author: Administrator
"""
import matplotlib.pyplot as plt
from scipy.misc import imread
from wordcloud import WordCloud,STOPWORDS
import xlrd

def set_background(picpath):
  back_coloring = imread(picpath)# 設(shè)置背景圖片
  return back_coloring

def create_word_cloud(txt_str, back_coloring):
  print('---- 根據(jù)詞頻,開始生成詞云! ----')
  font = r'C:\Windows\Fonts\simsun.ttc'
  wc = WordCloud(
    font_path=font,
    collocations=False, # 去重
    stopwords=STOPWORDS,
    max_words=100,
    width=2000,
    height=1200,
    # background_color='white',
    mask=back_coloring,
  )
  wordcloud = wc.generate(txt_str)
  # 寫詞云圖片
  wordcloud.to_file(".\wordcloud_test.png")
  # 顯示詞云文件
  plt.imshow(wordcloud)
  plt.axis("off")
  plt.show()

def read_from_xls(filepath,index_sheet):
  #讀取文件名
  #讀取表格#
  # 設(shè)置GBK編碼
  xlrd.Book.encoding = "gbk"
  rb = xlrd.open_workbook(filepath)
  print(rb)

  sheet = rb.sheet_by_index(index_sheet)
  nrows = sheet.nrows
  data_tmp = []

  for i in range(nrows - 1):
    tt=i+1
    tmp_char = [str(sheet.cell_value(tt,0))]
    tmp_num = int(sheet.cell_value(tt,1))
    data_tmp.extend(tmp_char*tmp_num)
  return data_tmp

if __name__ == '__main__': 
  picpath = r".\xxx.png"
  back_coloring = set_background(picpath)
  data_dic = read_from_xls(r'D:\Python_workspace\spyder_space\jieba分詞表.xlsx',0)
  data_dic_str = '\n'.join(data_dic)
  
#  with open(r".\jieba_分詞數(shù)據(jù).txt", "r",encoding='utf-8') as f: 
#    remove_stop_str = f.read() 

  create_word_cloud(data_dic_str, back_coloring)

當(dāng)然繪制詞云圖的方法有很多,這只是其中的一種

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Pandas常用累計、同比、環(huán)比等統(tǒng)計方法實踐過程

    Pandas常用累計、同比、環(huán)比等統(tǒng)計方法實踐過程

    這篇文章主要介紹了Pandas常用累計、同比、環(huán)比等統(tǒng)計方法實踐過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • pycharm中使用request和Pytest進行接口測試的方法

    pycharm中使用request和Pytest進行接口測試的方法

    這篇文章主要介紹了pycharm中使用request和Pytest進行接口測試的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 一步步教你用Python畫五彩氣球

    一步步教你用Python畫五彩氣球

    這篇文章主要給大家介紹了關(guān)于如何用Python畫五彩氣球的相關(guān)資料,主要是用turtle庫自帶的畫筆turtle.Turtle()來繪制氣球,文中給出了詳細的實例代碼,需要的朋友可以參考下
    2023-06-06
  • python實現(xiàn)月食效果實例代碼

    python實現(xiàn)月食效果實例代碼

    在本文里小編給大家整理了關(guān)于python實現(xiàn)月食效果的相關(guān)實例內(nèi)容以及對應(yīng)代碼,有興趣的朋友們學(xué)習(xí)下。
    2019-06-06
  • Python算法模塊之hashlib模塊詳解

    Python算法模塊之hashlib模塊詳解

    這篇文章主要介紹了Python算法模塊之hashlib模塊詳解,hash是一種算法,不同的hash算法只是復(fù)雜度不一樣,該算法接受傳入的內(nèi)容,經(jīng)過運算得到一串hash值,本文提供了部分實例代碼方便理解,需要的朋友可以參考下
    2023-08-08
  • python開發(fā)入門——列表生成式

    python開發(fā)入門——列表生成式

    這篇文章主要介紹了python 列表生成式的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python開發(fā),感興趣的朋友可以了解下
    2020-09-09
  • Python計算質(zhì)數(shù)的方法總結(jié)

    Python計算質(zhì)數(shù)的方法總結(jié)

    質(zhì)數(shù)(Prime?Number)是指大于1且只能被1和自身整除的正整數(shù),計算質(zhì)數(shù)是數(shù)論中的一個經(jīng)典問題,本文將介紹python中多種計算質(zhì)數(shù)的方法,希望對大家有所幫助
    2023-11-11
  • python中bisect模塊用法實例

    python中bisect模塊用法實例

    這篇文章主要介紹了python中bisect模塊用法實例,以實例形式介紹了bisect模塊中幾種常見函數(shù)的用法,非常具有實用價值,需要的朋友可以參考下
    2014-09-09
  • Keras多線程機制與flask多線程沖突的解決方案

    Keras多線程機制與flask多線程沖突的解決方案

    這篇文章主要介紹了Keras多線程機制與flask多線程沖突的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python中除法使用的注意事項

    Python中除法使用的注意事項

    這篇文章主要介紹了Python中除法使用的注意事項,是Python程序設(shè)計很重要的技巧,需要的朋友可以參考下
    2014-08-08

最新評論